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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

passing session variable into control block in template


Go to End


5 Posts   4384 Views

Avatar
danzzz

Community Member, 175 Posts

3 March 2009 at 9:12pm

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

Community Member, 791 Posts

4 March 2009 at 12:43am

Welcome to Sliverstripe !

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

e.g. $Top.Pid

Good luck !

Avatar
danzzz

Community Member, 175 Posts

4 March 2009 at 12:49am

hi

that's it :-)

thank you

daniel

Avatar
Sam

Administrator, 690 Posts

4 March 2009 at 2:28pm

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

Community Member, 1 Post

10 February 2011 at 10:35am

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;
}