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

[Solved] Condition does not work


Reply

6 Posts   1658 Views

Avatar
clauer

25 March 2009 at 11:12am (Last edited: 25 March 2009 9:18pm), Community Member, 12 Posts

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

Avatar
Double-A-Ron

25 March 2009 at 2:44pm Community Member, 604 Posts

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

Avatar
clauer

25 March 2009 at 9:18pm Community Member, 12 Posts

Hi,

thanks for your quick help.

Regards

Avatar
Double-A-Ron

25 March 2009 at 11:51pm Community Member, 604 Posts

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

Avatar
clauer

26 March 2009 at 1:56am Community Member, 12 Posts

Hi Aron,

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

Regards
Christian

Avatar
clauer

26 March 2009 at 2:19am Community Member, 12 Posts

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