bminer / node-blade

Blade - HTML Template Compiler, inspired by Jade & Haml
Other
320 stars 28 forks source link

Replacing a block does not prevent its original content from trying to render #108

Closed mhuebert closed 12 years ago

mhuebert commented 12 years ago

I get a "posts is not defined" error with the following structure (it works fine using template inheritance in jade).

Is it possible to replace a block & prevent the "original" version from trying to render, or is this an unavoidable side-effect of the way that blocks work in blade (vs. inheritance)?

in layout.html:

block content
  section
    foreach posts as post
      h1
        a(href=post.permalink) #{post.title}
      !#{post.content}

in viewPost.html:

include "../layout"

replace block content
  section
    h1 #{post.title}
      != post.content

I could fix this particular example by making the content block in layout.blade empty, and creating a separate index.blade template to list posts. It just seems odd that my rendering context must include all variables necessary for every block in my templates, even those that are to be replaced.

On a side note, I would also love "not defined" errors to fail silently... :)

bminer commented 12 years ago

@mhuebert - I'm not quite sure that I understand the problem. If you include a file that defines a block, you can use replace block to replace its content, just like template inheritance in Jade. You don't need the block to be empty in your layout file.

Also, it was a design decision to throw any "block not defined" errors and interrupt the rendering process. You could check __.blocks.content to see if the content block exists before trying to replace it. Honestly, though, I think throwing the error is better for debugging rather than trying to figure out why your block is not working right.

mhuebert commented 12 years ago

The problem is that when I render my "child" template, I don't provide the variables necessary for the original contents of the block in the "parent" to render.

On 2012-12-05, at 6:38 PM, Blake Miner notifications@github.com wrote:

@mhuebert - I'm not quite sure that I understand the problem. If you include a file that defines a block, you can use replace block to replace its content, just like template inheritance in Jade. You don't need the block to be empty in your layout file.

Also, it was a design decision to throw any "block not defined" errors and interrupt the rendering process. You could check __.blocks.content to see if the content block exists before trying to replace it. Honestly, though, I think throwing the error is better for debugging rather than trying to figure out why your block is not working right.

— Reply to this email directly or view it on GitHub.

bminer commented 12 years ago

@mhuebert - Ah! Yes... sorry I misread your initial post. Yeah... I don't think there is much we can do about that. Append and prepend statements need to know what the initial content was in order to render properly; that's why the initial block content is rendered... even if a replace block statement follows.

A good workaround is to check for the existence of posts in your initial block. Something like...

block content
  section
    - if(locals.posts)
      foreach posts as post
        h1
          a(href=post.permalink) #{post.title}
        !#{post.content}
bminer commented 12 years ago

Closing this issue for now