Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » Problem with <%if > control

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

Page: 1 2 3
Go to End
Author Topic: 8577 Views
  • Juanitou
    Avatar
    Community Member
    323 Posts

    Re: Problem with <%if > control Link to this post

    Hi!

    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!

    Regards,
    Juan

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: Problem with <%if > control Link to this post

    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.

  • J2-Paul
    Avatar
    Community Member
    51 Posts

    Re: Problem with <%if > control Link to this post

    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!

  • Tonyair
    Avatar
    Community Member
    81 Posts

    Re: Problem with <%if > control Link to this post

    Guys, how I can realise?

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

  • edk
    Avatar
    Community Member
    39 Posts

    Re: Problem with <%if > control Link to this post

    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.

  • Tonyair
    Avatar
    Community Member
    81 Posts

    Re: Problem with <%if > control Link to this post

    thx, i just thought about something more clearer =)

  • Willr
    Avatar
    Forum Moderator
    5502 Posts

    Re: Problem with <%if > control Link to this post

    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 %>

  • edk
    Avatar
    Community Member
    39 Posts

    Re: Problem with <%if > control Link to this post

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

    - Ed

    Edit: Just found a mention here in the new api docs...
    http://api.silverstripe.org/2.4/sapphire/view/ViewableData.html

    8577 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.