Jump to:

3393 Posts in 976 Topics by 852 members

Template Questions

SilverStripe Forums » Template Questions » Template "slots" for inserting markup in other templates

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

Page: 1
Go to End
Author Topic: 963 Views
  • GuillaumeC
    Avatar
    Community Member
    5 Posts

    Template "slots" for inserting markup in other templates Link to this post

    Hi,

    Does SilverStripe contain a way to add markup from a template to another template? Symfony calls it template slots, and Ruby on Rails content_for.

    What I'm looking to do specifically is to add HTML / Javascript bits to the bottom of a site layout from inside a view.

    I had a look at the reference pages of doc.silverstripe.com and the Sapphire API, and couldn't find an answer. I'm also quite new to SilverStripe, so I don't fully understand the capabilities / limitations of SSViewer yet...

    Thanks for your help!

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Template "slots" for inserting markup in other templates Link to this post

    The symfony slots act a little bit like the <% include Template %> functionality but includes are much more simpler.

    In sapphire you can also define your own functions which render into a template, then you can swap the template it renders into based on controller actions etc.

    For example if you have a extra totally different layout based on whether someFunctionFoo() is true or not you can do

    // main template
    $SomeMoreTemplate

    // in that templates controller
    function SomeMoreTemplate() {
    return ($this->someFunctionFoo()) ? $this->renderWith('Template1') : $this->renderWith('Template2');
    }

  • GuillaumeC
    Avatar
    Community Member
    5 Posts

    Re: Template "slots" for inserting markup in other templates Link to this post

    Thanks for your reply Willr!

    It's good to know that I can define my own template functions, but in my specific case it seems to be overkill. It often happens that I have view-specific Javascript to render, and while I could add it straight into the view, I would much prefer have some kind of mechanism to "inject" it to the bottom of my layout. A bit like that:

    I know I should probably just put that JS in an external file and add it with Requirements::javascript, but it shows well what I'm trying to achieve.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Template "slots" for inserting markup in other templates Link to this post

    I don't *think* there is anything to support that sort of dynamic injection yet. The current 2.4 SSViewer is little more than a simply string search and replace and doesn't really have any sort of smarts, hence using PHP and renderWith() which you can overload on your controller actions etc.

    SSViewer is getting a bit old overhaul for 3.0 so implementing functions like that may become easier.

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