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.

Archive /

Our old forums are still available as a read-only archive.

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

Templating: how to access $REQUEST variables?


Reply


5 Posts   3531 Views

Avatar
pIscIs

Community Member, 8 Posts

24 April 2008 at 5:49pm

Hi,

i building my first silverstripe portal and stuck at the question how to access $REQUEST variables(like $_GET, $_POST, $_SESSION) in templates? lets say, smarty has reserved variable $smarty in witch i can access these vars. I tried do functions in SS to retrieve these vars, like
function listGET() {
return $_GET;
}
In the template $listGET.foo show the value of $_GET['foo'] var, but how then it use in if statments? <% if a = listGET.foo %> returns a parse error about unexpected "}" at template.
Any help please.

Avatar
Willr

Forum Moderator, 5513 Posts

24 April 2008 at 6:31pm

I dont know if this is the proper way to do it but if I need to access a GET var or anything I just create a method and have that return my varible. for example function PageID { return $_GET['PageID']; } (ive removed all the issets/ and checking for the sake of this. And then in the template you can use $PageID to access that GET var. <% 2 = $PageID %>

Avatar
pIscIs

Community Member, 8 Posts

24 April 2008 at 8:44pm

The way is proper if you have some variable you must get in template, but lets say we have a filter form with a lot of select fields which has option loaded from control(DB), so how then check which option is selected in filter when we displaying its results? Now im using a basic function which adds and extra field in object from db:
function selectedControl(&$object, $selected, $var) {
if(empty($selected)) return;
foreach ($object as $item)
if($item->$var == $selected) $item->Selected = " selected=\"selected\"";      
}

but in this way we have two loops(foreach and control, in big amount of data, this will be slowly) and this way isnt very comfortable.
So the question would be - has SSViewer ability to parse array or object in if statment?

Avatar
Sam

Administrator, 685 Posts

25 April 2008 at 2:41pm

The reason that you're finding this a little difficult is that we don't really encourage this style of development with SilverStripe. You should keep your logic in your Controller or DataObject layers, and use the templates to describe how the HTML and CSS should be put together.

Avatar
pIscIs

Community Member, 8 Posts

25 April 2008 at 7:55pm

yeah, its true, and after hard work yesterday i understood how to prepare objects in this way, the trouble was, that until SS i worked with commercial CMS with coded source, so all objects were prepared for template from core and then the smarty was very good in this case because it can work with arrays in all ways. now i see templating way it should be. Thanks.