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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Template Questions /

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

Custom vars in both PHP and SS templates


Go to End


9 Posts   4492 Views

Avatar
Junglefish

Community Member, 109 Posts

10 June 2010 at 9:43pm

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

Community Member, 135 Posts

12 June 2010 at 12:41am

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

Community Member, 109 Posts

14 June 2010 at 9:52pm

Edited: 14/06/2010 9:52pm

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

Community Member, 109 Posts

15 June 2010 at 7:38pm

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

Community Member, 109 Posts

17 June 2010 at 9:48pm

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

Community Member, 109 Posts

21 June 2010 at 8:08pm

^^ bump ^^

Avatar
3dgoo

Community Member, 135 Posts

25 June 2010 at 1:22pm

Edited: 25/06/2010 1:23pm

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

Community Member, 109 Posts

25 June 2010 at 7:27pm

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