1266 Posts in 351 Topics by 486 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1477 Views |
-
Variable working in page.ss but not in include?

13 October 2009 at 2:01am
Hi,
I have a template variable, $PageType that is returned from the following code in my page classes (in /mysite/Code/):
public function PageType() {
return 'home'; // varies from page type to page type
}I then have the following code in /themes/mytheme/page.ss
<% if PageType %>
<div id="Layout" class="Layout-$PageType">
$Layout
</div>
<% else %>
<div id="Layout-home">
$Layout
</div>
This works fine, and depending on the page type, I get the right colour for the background etc.However, if I try to use $PageType in /themes/mytheme/includes/Navigation.ss, the variable just returns a blank value:
<ul>
<% control Menu(1) %>
<li>
<a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode">
<span class="navigation-class-$PageType">$MenuTitle.XML</span>
</a>
</li>
<% end_control %>
</ul>
Renders:<span class="navigation-class-">VBP Home Page</span>
I tried moving the navigation code straight into Page.ss, but to no avail. I also tried adding curly brackets around $PageType, but that also had no effect.
Could somebody point out exactly what I am doing wrong? I have read the tutorials but I can't seem to find a reference to this issue, although of course it may be there and I missed it.
Many thanks in advance for your help. This is my first SS site and I must say I am really impressed by how clean the extensibility approach is. Very nice.
-
Re: Variable working in page.ss but not in include?

13 October 2009 at 3:19am
I've found a solution, although I still don't know why the above does not work.
There is a Silverstripe-provided variable available called $RecordClassName
Using this instead of the string literal I was sending via my function works in the control segment.
Hope this helps anyone else who may have this issue.
-
Re: Variable working in page.ss but not in include?

13 October 2009 at 3:21am
Did you put the $PageType on the controller or in the model? Don't think it'll work if it's on the controller...
Also, note that you can use the variable $ClassName in yr templates which displays the type (so 'Page', or 'NewsPage' etc...)
And do not forget to flush
-
Re: Variable working in page.ss but not in include?

13 October 2009 at 3:56am
Hi,
Thanks for the reply. I put it in the controller. It worked OK for the reference in Page.ss, just not in the <%control %> in Navigation.ss. Odd.
But $ClassName actually sounds perfect - seems like I was replicating existing functionality! Many thanks for pointing that out
-
Re: Variable working in page.ss but not in include?

13 October 2009 at 4:04am
Yeah, if you want variables to work in an environment other than the direct view of the controller you need to put them in the model, then it'll work.
But of course, for this, $ClassName will do.
| 1477 Views | ||
|
Page:
1
|
Go to Top |


