A <% wrap %> method for template parser

36 views
Skip to first unread message

Simon Erkelens

unread,
Mar 24, 2016, 11:12:38 PM3/24/16
to SilverStripe Core Development
A lot of the template parsing mechanisms, support some sort of conditional wrapping.

For example, if an image exists, include the image, otherwise, don't.

In SilverStripe, I have to check with an if-statement.

Also, an often called feature is the link. If a link exist, wrap the text in an <a> tag, otherwise, just return the link.

Now, it requires <% if $Link %><a href="$Link"><% end_if %>Link text<% if $Link %></a><% end_if %>.

I suggest a <% wrap "a", "Link" %>Link text<% end_wrap %> method, which will do the same, but with less tags/ifs/conditionals.

The function itself, should take the first as the wrapping item, regardless of what it is. The second argument should be what to test for. And, thus, returning a wrapped string, if there is a Link method that returns something useful, otherwise, just return the string.

Zenmonkey

unread,
Mar 24, 2016, 11:58:47 PM3/24/16
to SilverStripe Core Development
Unfortunatly, you lose a lot of funcionality, or introduce a lot more complex syntax, or extra custom methods. A link or an image has lot more properties that would need to be passed for it work fully, especially when dealing with responsive images and accessible markup. I realize your example is simplified, but it still moves HTML genreation to the PHP method instead of the template markup. It was my understanding the design intent of the templating system is to keep controller and view seperate.

Stevie Mayhew

unread,
Mar 28, 2016, 3:20:43 PM3/28/16
to SilverStripe Core Development
You can use something like https://github.com/heyday/silverstripe-reusablehtml to achieve this if you want :)
Reply all
Reply to author
Forward
0 new messages