Jump to:

1384 Posts in 493 Topics by 522 members

Themes

SilverStripe Forums » Themes » Variable working in page.ss but not in include?

Discuss SilverStripe Themes.

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

Page: 1
Go to End
Author Topic: 1666 Views
  • Sparrowhawk
    Avatar
    Community Member
    33 Posts

    Variable working in page.ss but not in include? Link to this post

    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.

  • Sparrowhawk
    Avatar
    Community Member
    33 Posts

    Re: Variable working in page.ss but not in include? Link to this post

    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.

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Variable working in page.ss but not in include? Link to this post

    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

  • Sparrowhawk
    Avatar
    Community Member
    33 Posts

    Re: Variable working in page.ss but not in include? Link to this post

    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

  • dio5
    Avatar
    Community Member
    501 Posts

    Re: Variable working in page.ss but not in include? Link to this post

    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.

    1666 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.