shannonmoeller / handlebars-layouts

Handlebars helpers which implement layout blocks similar to Jinja, Nunjucks (Swig), Pug (Jade), and Twig.
http://npm.im/handlebars-layouts
MIT License
361 stars 29 forks source link

Append #42

Closed ghost closed 5 years ago

ghost commented 5 years ago

I have a layout like this:

Hello From layout.hbs
{{#block "styles"}}{{/block}}
{{#block "from-page"}}{{/block}}

and a page like this:

{{#extend "layout"}}
  {{#content 'styles' mode='append'}}
    <link href="/css/foo.css" rel="stylesheet">
  {{/content}}
  {{#content "from-page"}}
    Hello from page.hbs
    {{#embed "component"}}{{/embed}}
  {{/content}}
{{/extend}}

and a component like this:

Hello from component.hbs
{{#content 'styles' mode='append'}}
  <link href="/css/bar.css" rel="stylesheet">
{{/content}}

I'd like the component styles to append to the layout section, bubbling up through the page.

Is there a way to do this?

shannonmoeller commented 5 years ago

Unfortunately no, but I can see how that would be handy. One of the problems you'd face with this pattern is that if you used a component multiple times on a page you'd end up with the same stylesheet being <link>ed multiple times.

shannonmoeller commented 5 years ago

I think implementing this would lead to unexpected behaviors, would require dedupe logic, and it probably better handled by the data being fed into the templating system than the templating system itself.