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

Custom vars in both PHP and SS templates


Go to End
Reply

9 Posts   3213 Views

Avatar
Junglefish

10 June 2010 at 9:43pm Community Member, 104 Posts

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/

Avatar
3dgoo

12 June 2010 at 12:41am Community Member, 133 Posts

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;

Avatar
Junglefish

14 June 2010 at 9:52pm (Last edited: 14 June 2010 9:52pm), Community Member, 104 Posts

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/

Avatar
Junglefish

15 June 2010 at 7:38pm Community Member, 104 Posts

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/

Avatar
Junglefish

17 June 2010 at 9:48pm Community Member, 104 Posts

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/

Avatar
Junglefish

21 June 2010 at 8:08pm Community Member, 104 Posts

^^ bump ^^

Avatar
3dgoo

25 June 2010 at 1:22pm (Last edited: 25 June 2010 1:23pm), Community Member, 133 Posts

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

Avatar
Junglefish

25 June 2010 at 7:27pm Community Member, 104 Posts

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/

Go to Top