Time saver: withif and loopif blocks

64 views
Skip to first unread message

Jonathon Menz

unread,
Jan 4, 2016, 6:10:09 PM1/4/16
to SilverStripe Core Development
Hi all, hope you had a great holiday season.

Looking at introducing a little time-saving template syntax and wondering if anyone has feedback on it. 

Before

<% if $Thing %>
   
<% with $Thing %>
        $SomeProperty
   
<% end_with %>
<% end_if %>

<% if $Thing %>
   
<% loop $Thing %>
        $SomeProperty
   
<% end_loop %>
<% end_if %>


After

<% withif $Thing %>
    $SomeProperty
<% end_withif %>

<% loopif $Thing %>
    $SomeProperty
<% end_loopif %>

Patrick Nelson

unread,
Jan 4, 2016, 6:27:03 PM1/4/16
to silverst...@googlegroups.com
First time I'm seeing this, so I'll suggest maybe an alternative. Instead of creating more custom tags, would it be simpler to allow the ability to incorporate a little syntactic sugar into the <% if %> tag on it's own without adding a new tag? 

Maybe call it "sub-tag"? For example:

<% if $Thing with $Thing %>
    $SomeProperty
<% end_if %>

<% if $Thing loop $thing %>
    $SomeProperty
<% end_if %>

In other words, if you start a block in the second parameter of an "if" tag, that block will terminate at the same time as the "if" tag that it was instantiated in. To me this is familiar, since it reads like an extension of existing c-style programming if you were to omit the initial opening brace. The only difference is that the "if" would own the closing tag instead of the "with" or "loop" tag which is being called as a sub-tag of "if".

- Patrick


--
You received this message because you are subscribed to the Google Groups "SilverStripe Core Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to silverstripe-d...@googlegroups.com.
To post to this group, send email to silverst...@googlegroups.com.
Visit this group at https://groups.google.com/group/silverstripe-dev.
For more options, visit https://groups.google.com/d/optout.

Patrick Nelson

unread,
Jan 4, 2016, 6:28:58 PM1/4/16
to silverst...@googlegroups.com
p.s. I'm aware that the $Thing here is being mentioned redundantly, but it's an extension of the concept since in this case you're starting a new loop, but we're not assuming what you are looping out. This may be more powerful in the longer term. In fact, the "short" syntax may imply "$Thing" if it is not mentioned and we can simply start out with compatibility currently only being for "with" and "loop" sub-tags, for example.

Zenmonkey

unread,
Jan 4, 2016, 7:58:11 PM1/4/16
to SilverStripe Core Development
Personally I'd prefer that <% with %> and <% loop %> had implicit ifs so they'd fail gracefully and ignore the block if there was no object.

Sam Minnée

unread,
Jan 4, 2016, 8:03:25 PM1/4/16
to SilverStripe Core Development
Zenmonkey's suggestion is my preference, with the ability to do use else with them as well:

<% with $X %>  $Title <% else %>  not found <% end_with %> 
<% loop $Items > $Title<br> <% else %> no items <% end_loop %>

Note that more discussion is on the GitHub PR.

--

Christopher Pitt

unread,
Jan 4, 2016, 9:26:46 PM1/4/16
to SilverStripe Core Development
I prefer:
<% with $X %>  $Title <% else %>  not found <% end_with %> 
<% loop $Items > $Title<br> <% else %> no items <% end_loop %>

Here's a similar approach in Blade: https://laravel.com/docs/5.2/blade#control-structures

Handlebars and Mustache ignore empty lists/hashes. 

Christopher Pitt

unread,
Jan 4, 2016, 9:28:01 PM1/4/16
to SilverStripe Core Development
Forgot to mention: http://mozilla.github.io/nunjucks/templating.html#for also supports else syntax.

Florian Thoma

unread,
Jan 5, 2016, 5:19:59 PM1/5/16
to SilverStripe Core Development
+1 for

<% with $X %>  $Title <% else %>  not found <% end_with %> 
<% loop $Items > $Title<br> <% else %> no items <% end_loop %>


Nicolaas Thiemen Francken - Sunny Side Up

unread,
Jan 5, 2016, 7:26:43 PM1/5/16
to silverstripe-dev
​​

<% with $X %>  $Title <% else %>  not found <% end_with %> 
<% loop $Items > $Title<br> <% else %> no items <% end_loop %>

​+1 ​
 

Cam Findlay

unread,
Jan 6, 2016, 3:09:01 PM1/6/16
to SilverStripe Core Development
+1 for ignoring block if no objects and the use of else. This it's a good approach and quite pragmatic.

Jonathon Menz

unread,
Jan 6, 2016, 3:23:18 PM1/6/16
to SilverStripe Core Development
Think we have a pretty clear consensus, thanks everyone!

James Pluck

unread,
Jan 10, 2016, 2:26:50 PM1/10/16
to silverst...@googlegroups.com

On 5 January 2016 at 14:03, Sam Minnée <s...@silverstripe.com> wrote:
<% with $X %>  $Title <% else %>  not found <% end_with %> 
<% loop $Items > $Title<br> <% else %> no items <% end_loop %>

+1

Kind regards

James Pluck (BSc)
Reply all
Reply to author
Forward
0 new messages