Jump to:

3444 Posts in 1030 Topics by 871 members

Template Questions

SilverStripe Forums » Template Questions » Custom vars in both PHP and SS templates

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

Page: 1 2
Go to End
Author Topic: 3186 Views
  • Junglefish
    Avatar
    Community Member
    104 Posts

    Custom vars in both PHP and SS templates Link to this post

    How do I create a variable that I can use in both PHP and SS templates?

    For example:

    In my Page.php I can write a simple function:
       public function LatestNewsID() {
          return 150;
       }

    This allows me to use that var in a SS template something like this:
    <% if LatestNewsID = 150 %>
       Do something with $LatestNewsID
    <% else %>
       Do something else
    <% end_if %>

    However, how do I reference the same variable in a PHP template?

    jf/

  • 3dgoo
    Avatar
    Community Member
    133 Posts

    Re: Custom vars in both PHP and SS templates Link to this post

    That same if statement in your Page.php would look like this:

    if ($this->LatestNewsID() == 150)
    {
       Do something with $this->LatestNewsID();
    }
    else
    {
       Do something else;
    }

    In your example, LatestNewsID is a function. If it were a variable you would call it with this:

    $this->LatestNewsID;

  • Junglefish
    Avatar
    Community Member
    104 Posts

    Re: Custom vars in both PHP and SS templates Link to this post

    Thanks for the help. But I have some odd behaviour going on on the SS template.

    As before, I have this in Page.php:
    public function LatestNewsID() {
    return 150;
    }

    And in one of my include templates, SideBar.ss, I have a looping control element, a bit like this:

    ($LatestNewsID)
    <% control Menu(1) %>
       [$ID] ** [$LatestNewsID]
       <% if Children %>
          <% control Children %>
             <li class="$LinkingMode"><a href="$Link" title="Go to the $Title.XML page" >$MenuTitle.XML</a></li>
          <% end_control %>
       <% end_if %>
    <% end_control %>
    ($LatestNewsID)

    Where it says $LatestNewsID in parenthesis, both before and after the control element, the value 150 prints to the page.
    However, inside the control element, where $LatestNewsID is written in square brackets, the value is null.

    Can I not call the function inside a control element?

    jf/

  • Junglefish
    Avatar
    Community Member
    104 Posts

    Re: Custom vars in both PHP and SS templates Link to this post

    Thanks Luke. Still got a problem though...

    This prints to the page fine:
    $ID ** $Top.LatestNewsID

    However, on the next line, if I try to test against the function:
    <% if ID != Top.LatestNewsID %>

    I get this error:
    Parse error: parse error in C:\WINDOWS\Temp\silverstripe-cacheC--wamp-www-OlafIntranet-SilverStripe
    \.cacheC..wamp.www.OlafIntranet.SilverStripe.themes.olafIntranet03.templates.Page.ss on line 324

    I tried deleting the cache file but it just recreated itself on the next page request.

    Is there a different way of referring to a function or variable that is in the "Top" scope whilst inside a control loop?

    jf/

  • Junglefish
    Avatar
    Community Member
    104 Posts

    Re: Custom vars in both PHP and SS templates Link to this post

    To recap...

    Here's my function:
       public function LatestNewsID() {
          return 150;
       }

    Outside of a control loop, this is fine:
    to print to the page: $LatestNewsID
    to check for a value: <% if LatestNewsID = 150 %>

    Inside of a control loop, this is fine:
    to print to the page: $Top.LatestNewsID

    But this produces a Parse error:
    to check for a value: <% if Top.LatestNewsID = 150 %>

    If I can't use dot notation inside <% %> brackets, how do I refer to my variable?

    jf/

  • Junglefish
    Avatar
    Community Member
    104 Posts

    Re: Custom vars in both PHP and SS templates Link to this post

    ^^ bump ^^

  • 3dgoo
    Avatar
    Community Member
    133 Posts

    Re: Custom vars in both PHP and SS templates Link to this post

    Yeah the limitations of if statements in the template are really frustrating.

    You can either have a comparison statement, or you can retrieve a sub property but not both.

    There are a few work arounds, I'm not sure if any will be appropriate to what you need.

    One thing you can do is wrap a control around the if statement like so:

    <% control Top %>
    <% if LatestNewsID == 150 %>

    <% end_if %>
    <% end_control %>

    You could also create a function, and call that, and do the comparisons in your php code:
    Template:

    <% if CheckLatestNewsID %>

    <% end_if %>

    Php:

    public function CheckLatestNewsID ()
    {
       return LatestNewsID() == 150;
    }

    Hopefully this gives you some ideas.

    Good luck Squire

  • Junglefish
    Avatar
    Community Member
    104 Posts

    Re: Custom vars in both PHP and SS templates Link to this post

    AmpedUp - Thanks for the suggestions, I think your last one would probably have worked fine.

    [For the record - I found an entirely different way of factoring the behaviour I needed so this became a bit irrelevent in the end, but it's good to know there was a solution if I had needed one]

    jf/

    3186 Views
Page: 1 2
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.