Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Dynamic copyright year

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

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

Page: 1
Go to End
Author Topic: 2768 Views
  • sonicparke
    Avatar
    74 Posts

    Dynamic copyright year Link to this post

    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?

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Dynamic copyright year Link to this post

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

    You can't use PHP in templates. (:

  • sonicparke
    Avatar
    74 Posts

    Re: Dynamic copyright year Link to this post

    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?

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Dynamic copyright year Link to this post

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

  • yitter
    Avatar
    Community Member
    10 Posts

    Re: Dynamic copyright year Link to this post

    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

  • yitter
    Avatar
    Community Member
    10 Posts

    Re: Dynamic copyright year Link to this post

    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

  • Matt
    Avatar
    Core Development Team
    84 Posts

    Re: Dynamic copyright year Link to this post

    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.

    2768 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.