21280 Posts in 5729 Topics by 2600 members
General Questions
SilverStripe Forums » General Questions » Manipulating Model from Controller Then Rendering OR Accessing Current Request from Model
General questions about getting started with SilverStripe that don't fit in any of the categories above.
Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 1585 Views |
-
Manipulating Model from Controller Then Rendering OR Accessing Current Request from Model

16 May 2009 at 8:16am Last edited: 16 May 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 -
Re: Manipulating Model from Controller Then Rendering OR Accessing Current Request from Model

16 May 2009 at 3:20pm Last edited: 16 May 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.
-
Re: Manipulating Model from Controller Then Rendering OR Accessing Current Request from Model

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 -
Re: Manipulating Model from Controller Then Rendering OR Accessing Current Request from Model

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.
-
Re: Manipulating Model from Controller Then Rendering OR Accessing Current Request from Model

20 May 2009 at 2:15am
Thank you for the help!
-
Re: Manipulating Model from Controller Then Rendering OR Accessing Current Request from Model

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');
| 1585 Views | ||
|
Page:
1
|
Go to Top |



