3214 Posts in 848 Topics by 810 members
| Go to End | Next > | |
| Author | Topic: | 2596 Views |
-
Custom vars in both PHP and SS templates

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/
-
Re: Custom vars in both PHP and SS templates

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;
-
Re: Custom vars in both PHP and SS templates

14 June 2010 at 9:52pm Last edited: 14 June 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/
-
Re: Custom vars in both PHP and SS templates

15 June 2010 at 4:47pm
Hi there, the control element enters a new scope, so that when you call LatestNewsID within the control element, what it's effectively calling is: Menu(1).LatestNewsID()
You'd need to do:
($LatestNewsID)
<% control Menu(1) %>
[$ID] ** [$Top.LatestNewsID]
<% if Children %>
. . .See here: http://doc.silverstripe.org/built-in-page-controls#top
Cheers,
- Luke -
Re: Custom vars in both PHP and SS templates

15 June 2010 at 7:38pm
Thanks Luke. Still got a problem though...
This prints to the page fine:
$ID ** $Top.LatestNewsIDHowever, 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 324I 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/
-
Re: Custom vars in both PHP and SS templates

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.LatestNewsIDBut 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/
-
Re: Custom vars in both PHP and SS templates

25 June 2010 at 1:22pm Last edited: 25 June 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
| 2596 Views | ||
| Go to Top | Next > |



