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.

Template Questions /

Calling an action with an url


Reply


3 Posts   3448 Views

Avatar
SilverRay

Community Member, 167 Posts

2 January 2009 at 9:50pm

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.

Avatar
Carbon Crayon

Community Member, 598 Posts

3 January 2009 at 2:16am

Edited: 03/01/2009 2:17am

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: [url]http://www.silverstripe.org/customising-the-cms/show/251362[/url]

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 :)

Avatar
SilverRay

Community Member, 167 Posts

3 January 2009 at 2:35am

Edited: 03/01/2009 2:35am

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.