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


Go to End
Reply


11 Posts   2096 Views

Avatar
neros3

Community Member, 51 Posts

28 October 2010 at 8:15pm

Hi there,

I've been thinking a bit about how to reuse a rendering.
Say, for instance, that I have a list of DataObjects that I want to render in a certain way, and I would like the rendering to be the exact same all over the site. On the frontpage, on the member page and so on. Perhaps I would like to use the same rendering on the same page but with to different lists (could be a list latest pages and just a list of pages). Both lists should look the exact same - and I dont like to repeat my code...

What I'm looking for is something like:

<% control MyCustomPageRenderer datasource="somedatasource" (should rather be an ID so that the controller could identify the control and bind the data to it) />

Any ideas on how get further with this?

Thanks!

Avatar
swaiba

Forum Moderator, 1798 Posts

28 October 2010 at 9:26pm

Firstly to "re-use accross the site" I'd recommend placing the template into an include, then using...

<% include TemplateName %>

...where it is required.

the problem with what you are asking, I think, is that you want to pass a dynamic variable into this and this is not possible. what you can do is have the controller specify the data source in a different implementation of MyCustomPageRenderer in each different controller.

Avatar
neros3

Community Member, 51 Posts

28 October 2010 at 9:43pm

Hi Swaiba,

Thanks for your feedback. The include would work if I only use the controller once on each page right?
But could you help me understand how to have the controller specify the data source in a different implementation?

Thanks

Avatar
swaiba

Forum Moderator, 1798 Posts

28 October 2010 at 9:47pm

in each different page controller specify the...

function MyCustomPageRenderer()
{
//build custom dataobjectset here...
//i.e.
return DataObject::get('MyObject');
}

then change the "get" in each different controller, this means the work on what "controls" the output stays in the controller and the "view" of that data is consistent in the template.

Avatar
neros3

Community Member, 51 Posts

28 October 2010 at 9:57pm

ok - I think I misunderstood then.
What if I would like to have the same rendering on the one page twice? then I could "bind" with data in the controller, but the name of the control <% control MyCustomRenderer %> would still be the same (since I can't change it, because I only want one)

It would nice to have something as <% control MyCustomRenderer as MyCustomRendererSecondList %>

and then in the code bind the other dataset that I want to display on the same page to the MyCustomRendererSecondList...

Avatar
swaiba

Forum Moderator, 1798 Posts

28 October 2010 at 10:05pm

I don't follow this at all...
<% control MyCustomRenderer as MyCustomRendererSecondList %>

you CAN pass a fixed string like...
<% control MyCustomRenderer(FIXEDSTRING) %>
or
<% control MyCustomRenderer(123) %>

but nothing "dynamic", so you could have...

<% control MyCustomRenderer(1) %>
<% control MyCustomRenderer(2) %>

Hope this makes sense, of course the best way to get your head round this is...

http://doc.silverstripe.org/tutorials
http://doc.silverstripe.org/templates

Avatar
neros3

Community Member, 51 Posts

28 October 2010 at 10:22pm

the control AS othercontrol_id was just pseudo code to explain my wish... or how this could be done.

When passing a variable I would do it in the markup of the control and not when "calling" the control right?
When using <% control MyControl %> I am about to define/declare the markup for some data provided via MyControl function in then Controller right?

Avatar
swaiba

Forum Moderator, 1798 Posts

28 October 2010 at 10:38pm

Sorry but I simply do not follow what you are asking... Maybe something is lost in translation here... but as I said I advise reading the docs and doing the tutorials... this is where you will get to understand templating... or if you are committed to ss get the book. When you have done this and have something working or close to doing as you wish how about you post that?

Go to Top