Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Calling an action with an url

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

Page: 1
Go to End
Author Topic: 3343 Views
  • SilverRay
    Avatar
    Community Member
    167 Posts

    Calling an action with an url Link to this post

    HNY!

    I remember I've read something somewhere in the forum about how to call an action with an url, but I can not find it... it might help me with the following.

    In Page.php I have a function in the controller section, like so:

       function showMensShirts() {
          $whereStatement = "MenCat = 1 AND ShirtCat = 1";
          return DataObject::get("Page", $whereStatement);
       }

    When I put a control block <% control showMensShirts %> in my template I can iterate over all pages where the mentioned variables are true, and then render whatever desired information. But I recall you can use "showMensShirts" in the url and as such call the function... but how would I use that in my template then? And how would I go about using more than one function in the same template?

    Thanks,
    - Ray.

  • Aram
    Avatar
    Community Member
    598 Posts

    Re: Calling an action with an url Link to this post

    Hi SilveRay, happy new year!

    I recently descovered the URL stuff when trying to make a veiw all button for my article holder. You can see my use of them on this thread: http://www.silverstripe.org/customising-the-cms/show/251362

    As far as I understand it anything that is after the normal URL are broken down into URL parameters, in the form of /action/ID/otherID.

    So you can call a function by adding that function name as the action on the end of the URL and then if you wanted to pass some properties in to it you could do so with the ID and Other ID.

    So for example in my code for the veiw all button (which was heavily 'inspired' by UncleCheeses Image Gallery code) I use a function called ViewAll() which checks for the value of the Action and if it is 'showall' then it returns 1. I can then use this function both in the template and other functions in the controller:

    function ViewAll() {
    $params = Director::urlParams();
    return (isset($params['Action']) && $params['Action'] == 'showall');
    }

    So in the template I can use <% if viewAll %> do something <% end_if %> and in the controller I can have an if statement like if($this->ViewAll()){ do something }

    Thats about as far as I have got with them so far

  • SilverRay
    Avatar
    Community Member
    167 Posts

    Re: Calling an action with an url Link to this post

    Hey thanks aram, that's what I was looking for I think. I will check what you referred to and report back when stuck ;)

    - Ray.

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