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.

Data Model Questions

passing session variable into control block in template


Reply

5 Posts   3269 Views

Avatar
danzzz

3 March 2009 at 9:12pm Community Member, 175 Posts

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

Avatar
Fuzz10

4 March 2009 at 12:43am Community Member, 787 Posts

Welcome to Sliverstripe !

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

e.g. $Top.Pid

Good luck !

Avatar
danzzz

4 March 2009 at 12:49am Community Member, 175 Posts

hi

that's it :-)

thank you

daniel

Avatar
Sam

4 March 2009 at 2:28pm Administrator, 685 Posts

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. ;-)

Avatar
Stams

10 February 2011 at 10:35am Community Member, 1 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;
}