Jump to:

3434 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » Re-use html-rendering / template

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

Page: 1 2
Go to End
Author Topic: 2052 Views
  • neros3
    Avatar
    Community Member
    51 Posts

    Re: Re-use html-rendering / template Link to this post

    ok - allow me to enter the sandbox and then get back to you.

    main goal is:

    1. never write the same markup twice. A list of MyObjects should always look the same, so no need to duplicate the markup.
    2. being able to display same data twice without breaking goal number 1.

    Actually its the exact same functionality that asp.net provides with user controls that im looking for ( Html.RenderPartial("mycontrol", somedata) )

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: Re-use html-rendering / template Link to this post

    You can use function forTemplate() on a DataObject to specify a template for the DataObject.

    Another way is to add a custom methods to you DataObjects and use renderwith to assign a template to the view

    function MyView(){
    return $this->renderWith('MyViewTemplate');
    }

    In your template:

    <% control MyDOSet %>
    $MyView
    <% end_control %>

    See this thread on the Google Group channel:

    http://groups.google.com/group/silverstripe-dev/browse_thread/thread/0b0f299adb8177fd

  • neros3
    Avatar
    Community Member
    51 Posts

    Re: Re-use html-rendering / template Link to this post

    Hi Martijn

    Thank you so much for your reply! This was really helpful!
    I actually managed to get another solution going, where I render the complete list of objects in one control and not the one object at a time. But this is only a matter of having the control-call in the include template or in the actual view.

    Solution 1:

    public function ControllerGetAll() {
    $page = $this->customise(array(
    'Pages' => $this->GetAllPages()
    ));

    return $page->renderWith(array("PageList"));
    }

    PageList.ss
    <% control Pages %>
    $Title - ({$Link})
    <% end_control %>

    and used in the view like this:

    $ControllerGetAll

    But I actually like the other solution better - since its a bit cleaner.
    Solution 2

    Model describes a rendering for one object:
    public function MyView(){
    return $this->renderWith(array('PageRendering'));
    }

    in view:
    <% control GetAllPages %>
    $MyView
    <% end_control %>

    PageRendering.ss

    $Title - ({$Link})

    //the control-blocks are moved to the "main-view" instead of inside the included rendering

    The cool thing about solution 1 is that it can be used as a REST-ful ajax-call-include since it will give you the complete list rendering on www.yourdom.com/controller/ControllerGetAll !!

    2052 Views
Page: 1 2
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.