3093 Posts in 875 Topics by 654 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 2922 Views |
-
passing session variable into control block in template

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 -
Re: passing session variable into control block in template

4 March 2009 at 12:43am
Welcome to Sliverstripe !
You can reach the "parent"-context by using $Top ..
e.g. $Top.Pid
Good luck !
-
Re: passing session variable into control block in template

4 March 2009 at 12:49am
hi
that's it
thank you
daniel
-
Re: passing session variable into control block in template

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. ;-)
-
Re: passing session variable into control block in template

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;
}
| 2922 Views | ||
|
Page:
1
|
Go to Top |




