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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Manipulating Model from Controller Then Rendering OR Accessing Current Request from Model


Reply


6 Posts   2130 Views

Avatar
Ben Gribaudo

Community Member, 181 Posts

16 May 2009 at 8:16am

Edited: 16/05/2009 8:17am

Hello,

In a controller, is there a way to manipulate the model object then have SilverStripe render that model using the appropriate template?

I can get close to this by overriding handleRequest() in the controller, making whatever changes are desired to the model, then "return $this->renderWith(array("Page"), $this->dataRecord);". The problem with this approach is that the template name must be hard coded. It would be nice if there was a way to say "render this model object using the appropriate template."

-- or --

Is there a way which I can access the current request from the model object?

.

In case you are wondering, I am creating a page type which pulls its content from a remote web application based on the current request's URL. It's easy for the controller do to the fetching, as it has access to the request, but then the controller ends up being hard-coded to render using a certain template. In order to move this functionality to the model (easing the process of template rendering), the model needs access to the current request.

Thank you,
Ben

Avatar
Hamish

Community Member, 712 Posts

16 May 2009 at 3:20pm

Edited: 16/05/2009 3:21pm

A little confusing... where are you getting the template name from? If the template name is a property of the model, just use that:

For example:

   ...
   return $this->renderWith(array(
      $this->dataRecord->evaluatedTemplateName()
      ), $this->dataRecord);
   ...

or, if the template name is a static property of the model:

   ...
   return $this->renderWith(array(
      $this->dataRecord->stat('templateName')
      ), $this->dataRecord);
   ...

Don't move anything to model, let your controller pull the information from the model.

Avatar
Ben Gribaudo

Community Member, 181 Posts

18 May 2009 at 11:37pm

Hamish,

> where are you getting the template name from?

That's what I'm trying to figure out how to do. SilverStripe has some way to figure out what template to use to render a model. How would programmaticly access that knowledge?

Thanks,
Ben

Avatar
ajshort

Community Member, 244 Posts

19 May 2009 at 12:16am

That's what I'm trying to figure out how to do. SilverStripe has some way to figure out what template to use to render a model. How would programmaticly access that knowledge?

In order to get a SSViewer object with all the appropriate template names already set up, you can just call Controller->getViewer().

However, a nicer approach is to return an array from your action on your controller, for example:

return array (
   'Content' => $content
);

The Controller will then automatically render with the appropriate content, but the $Content template property will be set to the value you returned - as well as any other key/value pairs you specify.

Avatar
Ben Gribaudo

Community Member, 181 Posts

20 May 2009 at 2:15am

Thank you for the help!

Avatar
Ben Gribaudo

Community Member, 181 Posts

21 May 2009 at 4:40am

Ben Gribaudo: Is there a way which I can access the current request from the model object?

FYI, I found a way to access the current controller, and thus the current request, from the model:

$currentController = $this->CurrentPage();
$currentRequest = $this->CurrentPage()->request;
$urlParameterFromRequest = $this->CurrentPage()->request->getVar('url');