Jump to:

23490 Posts in 18996 Topics by 2878 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, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 2055 Views
  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Manipulating Model from Controller Then Rendering OR Accessing Current Request from Model Link to this post

    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

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Manipulating Model from Controller Then Rendering OR Accessing Current Request from Model Link to this post

    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.

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: Manipulating Model from Controller Then Rendering OR Accessing Current Request from Model Link to this post

    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

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: Manipulating Model from Controller Then Rendering OR Accessing Current Request from Model Link to this post

    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.

  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts
  • Ben Gribaudo
    Avatar
    Community Member
    181 Posts

    Re: Manipulating Model from Controller Then Rendering OR Accessing Current Request from Model Link to this post

    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');

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