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 /

Re-use html-rendering / template


Reply


11 Posts   2097 Views

Avatar
neros3

Community Member, 51 Posts

28 October 2010 at 10:46pm

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

Avatar
Martijn

Community Member, 271 Posts

29 October 2010 at 3:57pm

Edited: 29/10/2010 3:58pm

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

Avatar
neros3

Community Member, 51 Posts

31 October 2010 at 8:39pm

Edited: 31/10/2010 8:50pm

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 !!