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, biapar, Willr, Ingo, swaiba

[Solved] Condition does not work


Go to End
Reply


6 Posts   1865 Views

Avatar
clauer

Community Member, 12 Posts

25 March 2009 at 11:12am

Edited: 25/03/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 .= <<<SSVIEWER

Thanks for any help
Christian

Avatar
Double-A-Ron

Community Member, 604 Posts

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

Avatar
clauer

Community Member, 12 Posts

25 March 2009 at 9:18pm

Hi,

thanks for your quick help.

Regards

Avatar
Double-A-Ron

Community Member, 604 Posts

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

Avatar
clauer

Community Member, 12 Posts

26 March 2009 at 1:56am

Hi Aron,

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

Regards
Christian

Avatar
clauer

Community Member, 12 Posts

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