3214 Posts in 848 Topics by 810 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1445 Views |
-
[Solved] Condition does not work

25 March 2009 at 11:12am Last edited: 25 March 2009 9:18pm
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 .= <<<SSVIEWERThanks for any help
Christian -
Re: [Solved] Condition does not work

25 March 2009 at 2:44pm
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 %> -
Re: [Solved] Condition does not work

25 March 2009 at 9:18pm
Hi,
thanks for your quick help.
Regards
-
Re: [Solved] Condition does not work

25 March 2009 at 11:51pm
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 -
Re: [Solved] Condition does not work

26 March 2009 at 1:56am
Hi Aron,
thanks again, that's exactly what I was looking for...
Regards
Christian -
Re: [Solved] Condition does not work

26 March 2009 at 2:19am
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
| 1445 Views | ||
|
Page:
1
|
Go to Top |


