Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Templating: how to access $REQUEST variables?

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

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

Page: 1
Go to End
Author Topic: 3430 Views
  • pIscIs
    Avatar
    Community Member
    8 Posts

    Templating: how to access $REQUEST variables? Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5490 Posts

    Re: Templating: how to access $REQUEST variables? Link to this post

    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 %>

  • pIscIs
    Avatar
    Community Member
    8 Posts

    Re: Templating: how to access $REQUEST variables? Link to this post

    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?

  • Sam
    Avatar
    Administrator
    679 Posts

    Re: Templating: how to access $REQUEST variables? Link to this post

    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.

  • pIscIs
    Avatar
    Community Member
    8 Posts

    Re: Templating: how to access $REQUEST variables? Link to this post

    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.

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