Jump to:

3459 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » passing session variable into control block in template

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

Page: 1
Go to End
Author Topic: 3251 Views
  • danzzz
    Avatar
    Community Member
    175 Posts

    passing session variable into control block in template Link to this post

    hi,

    i have this function in my page.php:

    public function Pid() {
    return strip_tags(Session::get('PID'));
    }

    this functions returns me a session in all my pages. and then i have this code in my tempalte:

    <% control Children %>
    $Link
    <br/>
    <% end_control %>

    this just writes down some links. and now i want to attach the session value to this link and
    i tried this:

    <% control Children %>
    $Link$Pid
    <br/>
    <% end_control %>

    that dont work. i also recognized that i cant use $Pid in the control block, if i put $Pid somewhere
    outside it will display the correct session variable.

    how can i pass the session variable into this control block or how can i attach it the $Link?

    thx
    daniel

  • Fuzz10
    Avatar
    Community Member
    787 Posts

    Re: passing session variable into control block in template Link to this post

    Welcome to Sliverstripe !

    You can reach the "parent"-context by using $Top ..

    e.g. $Top.Pid

    Good luck !

  • danzzz
    Avatar
    Community Member
    175 Posts

    Re: passing session variable into control block in template Link to this post

    hi

    that's it

    thank you

    daniel

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: passing session variable into control block in template Link to this post

    The other thing you might want to consider doing is adding a PidLink() method to your Page class (not Page_Controller)

    function PidLink() {
    return $this->Link() . "?pid=" . urlencode(strip_tags(Session::get("PID")));
    }

    This will keep the logic of your URL crafting out of your view. For example, you just concatenated the link, and I've put it into a get variable. If you wanted to change the get variable, or you realised that $this->Link() sometimes returned a "?" and you needed to handle that, you only have to fix the bug in one place, instead of in every template.

    FYI: This is the reason why we like to keep our template language simple. ;-)

  • Stams
    Avatar
    Community Member
    1 Post

    Re: passing session variable into control block in template Link to this post

    Hi,

    How would I Iterate through an array within a control. For example:

    <% control Children %>
    <li><a href='$Top.counter' class="$LinkingMode">$Title</a></li>
    <% end_control %>

    function counter(){
       $results = array('position' => 1, 'position' => 2, 'position' => 3, 'position' => 4, 'position' => 5);
       return $results;
    }

    3251 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.