Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Problem with <%if > control

Go to End

19 Posts   12014 Views


Community Member, 323 Posts

8 June 2010 at 5:03am


No need to be sorry! I didn’t mean to seem angry. I’m not a native Englishman.

Some elements to remember for happy silverstriping: SS templates are not PHP, typos in the manual of an open-source project can be corrected by the users (as I just did) and there are far more lazy forum members than stupid forum questions, so keep researching and asking and you should get a lot of help here!



Community Member, 253 Posts

8 June 2010 at 9:09am

Yes, I don't think this was meant as an impatient response. I was simply saying thanks as I hadnt linked through to the Docs which would have saved me time re-typing them.

Hopefully you will find the SS community very helpful.


Community Member, 51 Posts

10 June 2010 at 6:54am

Thanks for the help. Thought it would be obvious. It take your point about the usage. I thought the same too. It will do for now while I learn. A better way will come to me I am sure!


Community Member, 81 Posts

18 June 2010 at 5:02am

Edited: 18/06/2010 5:18am

Guys, how I can realise?

<% if $Pos%7 != 0 %>
<% end_if %>


Community Member, 39 Posts

18 June 2010 at 8:05am

Edited: 18/06/2010 8:06am

The <% if %> control structures with Silverstripe are somewhat limited. Some don't like it - I think it is great. It keeps the presentation code as clean and abstracted from the logic. Your .ss templates are much cleaner (all be it for a mild inconvenience).

So if you can't solve your task with the simple Silverstripe Template logic there is a work around...and it is a perfect solution that I have seen mentioned of the forums a few times.

Basically what you need to do is create a method in your Page class file. (the .php file in the mysite directory/code directory)

1. Create the method in your Page's Controller class:

function checkSomething(){      
      if (compareThis != compareThat) {
       return false;
      } else {
         return true;

2. Now that you created the method 'checkSomething' in your class file (example like Page.php). Go back to your .ss template file and you can add something like.

<% if checkSomething %>
    <p>Hello World</p>
<% end_if %>

Just with that simple strtucture you can build out whatever you may need as complex as you like it within the Class file. When I first saw this it opened up a whole new world within Silverstripe.


Community Member, 81 Posts

18 June 2010 at 9:08am

thx, i just thought about something more clearer =)


Forum Moderator, 5523 Posts

18 June 2010 at 6:37pm

In 2.4 it brings the ability to use Mod in the templates. It has got 2 new methods MultipleOf($value) and Modulus($value, $offset) which I think you're trying to do Tonyair. So in your example you can do

<% if MultipleOf(7) %>
// do stuff
<% end_if %>


Community Member, 39 Posts

18 June 2010 at 6:43pm

Edited: 18/06/2010 6:46pm

Hi Willr, thanks for the heads up. Are these documented somewhere?

- Ed

Edit: Just found a mention here in the new api docs...