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.


Discuss SilverStripe Themes.

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

Variable working in but not in include?


5 Posts   1674 Views


13 October 2009 at 2:01am Community Member, 33 Posts


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/

<% if PageType %>
<div id="Layout" class="Layout-$PageType">
<% else %>
<div id="Layout-home">

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/, the variable just returns a blank value:

   <% control Menu(1) %>
<a href="$Link" title="Go to the $Title.XML page" class="$LinkingMode">
<span class="navigation-class-$PageType">$MenuTitle.XML</span>
   <% end_control %>


<span class="navigation-class-">VBP Home Page</span>

I tried moving the navigation code straight into, 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.


13 October 2009 at 3:19am Community Member, 33 Posts

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.


13 October 2009 at 3:21am Community Member, 501 Posts

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 :)


13 October 2009 at 3:56am Community Member, 33 Posts


Thanks for the reply. I put it in the controller. It worked OK for the reference in, just not in the <%control %> in Odd.

But $ClassName actually sounds perfect - seems like I was replicating existing functionality! Many thanks for pointing that out :)


13 October 2009 at 4:04am Community Member, 501 Posts

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.