Blocks / place holders in layouts #111

Open
ejsmith opened this Issue Feb 16, 2012 · 2 comments

3 participants

@ejsmith

I want to provide place holders for content in my layout template that I can implement in my child templates. I have tried everything I can think of but I can only specify string values to be placed in the place holders and not complete html sections.

layout

doctype 5
html lang: 'en', ->
  head ->
    title "#{@title} - Site" if @title?
    @headsection if @headsection?
body ->
  p 'some content'
  @body

child template

@title = 'blah'

@headsection = ->
  script src: 'blah'

p 'Child content'
@gradus

please see issue #110

@twilson63

As maurice once said, this functionality is usually implemented in frameworks, but you should be able to do it now. At least, in a flatiron plugin I created called creamer I am able to accomplish it, by sub dividing my template into local functions. See my test example:

  describe '#attach()', ->
    it 'should allow for content_for style yields', ->
      layout = ->
        html ->
          body ->
            content()
            @header() if @header?
            @body()
            @footer() if @footer?
      t = ->
        @header = -> h1 'Header'
        @body = -> p 'Hello World'
        @footer = -> footer 'Footer'
      output = htmlf """
<html>
  <body>
    <h1>Header</h1>
    <p>Hello World</p>
    <footer>Footer</footer>
  </body>
</html>
      """
      app = new broadway.App()
      app.use creamer, layout: if layout? then layout else null
      app.init()
      app.bind(t).should.equal output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment