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.

Archive

Our old forums are still available as a read-only archive.

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

Dynamic copyright year


Reply

7 Posts   2802 Views

Avatar
sonicparke

6 February 2008 at 11:05am 74 Posts

Howdy,

I thought I'd try to throw in some php for the year to be dynamic just to complicate things. But it doesn't want to work.

This is what I tried:

<p>Copyright &copy; <?php echo date("Y"); ?></p>

And this is the error I got:
Parse error: syntax error, unexpected T_ECHO in /tmp/silverstripe-cache-home-popraux-public_html/.cache.home.popraux.public_html.themes.popraux.templates.Page.ss on line 85

Do I need do use a certain control or varialble for this to work?

Avatar
simon_w

6 February 2008 at 11:17am Forum Moderator, 474 Posts

<p>Copyright &copy; $Now.Year</p>

You can't use PHP in templates. (:

Avatar
sonicparke

6 February 2008 at 11:41am 74 Posts

Yeah. After I did it I figured that was the case.

So how about a random array of variables? I'm hijacking my own thread but considering I can't do PHP in a template would I have to put the code to generate the random array in Page.php and use it's variables in the template?

Avatar
simon_w

6 February 2008 at 2:36pm Forum Moderator, 474 Posts

Make a method in either Page or Page_Controller, which returns the random value, and call it frm the template, using $Methodname

Avatar
yitter

8 February 2008 at 5:00am Community Member, 10 Posts

Does anyone know how I could make Template Variables like $Now.Year work in the Content Field of Pages? So that it is replaced when I write in $Now.Year in a pages Content. That would be quite cool!

cheers

Avatar
yitter

8 February 2008 at 11:03am (Last edited: 8 February 2008 11:07am), Community Member, 10 Posts

Found a solution ..

if you ever want to parse PHP via Template Tags inside $Content you could do the following:

class Page_Controller extends ContentController {

....

function ContentProcessed(){
   $s = new SSViewer_FromString($this->data()->getField("Content"));
   return $s->process($this);
}

....

}

if you now use $ContentProcessed in your Page.ss you can enter template variables in the CMS. $Now.Year in the "Content" Html Editor Field would turn into 2008 or whatever year we are on the Website. There may be something more elegant (thinking $Content.Processed ..?) but this works pretty well.

Hope that helps someone someday

Avatar
Matt

9 February 2008 at 7:32pm Core Development Team, 84 Posts

Extending that example a bit, you could also do this instead of your return line:
return DBField::create("HTMLText", $s->process($this));

That way, you can do things like $ContentProcessed.XML or $ContentProcessed.Summary in your template.