Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » [Solved] Condition does not work

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

Page: 1
Go to End
Author Topic: 1649 Views
  • clauer
    Avatar
    Community Member
    12 Posts

    [Solved] Condition does not work Link to this post

    Hi all,

    why does this not work?

    <% if Now.Year == 2009 %>
       Â© ($Now.Year)
    <% else %>
       Â© (2009 - $Now.Year)
    <% end_if %>

    The error message looks like this:

    Parse error: syntax error, unexpected '}' in C:\TEMP\silverstripe-cacheE--Internet-htdocs-SilverStripe\.cacheE..Internet.htdocs.SilverStripe.themes.cardio.templates.Page.ss on line 131

    And here is the generated code, the error message refers to the } before else:

                   <% if Now.Year == 2009 %>
                      (
    SSVIEWER;
    $val .= $item->obj("Now",null,true)->XML_val("Year",null,true) ;
    $val .= <<<SSVIEWER
    )
                   
    SSVIEWER;
    } else { ;
    $val .= <<<SSVIEWER

                      (2009 -
    SSVIEWER;
    $val .= $item->obj("Now",null,true)->XML_val("Year",null,true) ;
    $val .= <<<SSVIEWER
    )
                   
    SSVIEWER;
    } ;
    $val .= <<<SSVIEWER

    Thanks for any help
    Christian

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: [Solved] Condition does not work Link to this post

    Hi Mate,

    I'm pretty sure you can't do comparisons in the template as in:

    <% if Now.Year == 2009 %>

    Instead, in your page_controller, add a function that checks the value and returns true or false.

    function yearIs2009() {
          if(date('Y') == '2009') {
             return true;
          } else {
             return false;
          }
       }

    Then your template code will be:

    <% if yearIs2009 %>
    © ($Now.Year)
    <% else %>
    © (2009 - $Now.Year)
    <% end_if %>

  • clauer
    Avatar
    Community Member
    12 Posts

    Re: [Solved] Condition does not work Link to this post

    Hi,

    thanks for your quick help.

    Regards

  • Double-A-Ron
    Avatar
    Community Member
    604 Posts

    Re: [Solved] Condition does not work Link to this post

    Actually, looking at that code again, you might as well just have the function return the Copyright HTML

    function getCopyright() {
    if(date('Y') == '2009') {
    return '© 2009';
    } else {
    return '© (2009 - ' . date('Y') . ')' ;
    }
    }

    Then your template can just be:

    <% getCopyright %>

    Makes things tidier if you are using that block on multiple page types.

    Cheers
    Aaron

  • clauer
    Avatar
    Community Member
    12 Posts

    Re: [Solved] Condition does not work Link to this post

    Hi Aron,

    thanks again, that's exactly what I was looking for...

    Regards
    Christian

  • clauer
    Avatar
    Community Member
    12 Posts

    Re: [Solved] Condition does not work Link to this post

    Thanks again. I changed your proposition and added $thisYear as a parameter which represents the initial year which then must only be declared within the template:

    function getCopyright($thisYear) {
    if(date('Y') == $thisYear) {
    return '© ' . $thisYear;
    } else {
    return '© (' . $thisYear . ' - ' . date('Y') . ')' ;
    }
    }

    In the template it then must look like this:

    $getCopyright(2009)

    Regards
    Christian

    1649 Views
Page: 1
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.