Jump to:

3393 Posts in 976 Topics by 852 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: 1985 Views
  • neros3
    Avatar
    Community Member
    51 Posts

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

    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!

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

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

    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.

  • neros3
    Avatar
    Community Member
    51 Posts

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

    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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

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

    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.

  • neros3
    Avatar
    Community Member
    51 Posts

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

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

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

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

    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

  • neros3
    Avatar
    Community Member
    51 Posts

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

    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?

  • swaiba
    Avatar
    Forum Moderator
    1769 Posts

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

    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?

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