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

Add params to #embed #14

Closed baldore closed 9 years ago

baldore commented 9 years ago

Hi, is there a way to add params or data to the embeds, just like this???

Component

<div>
  <h2>I'm visible</h2>
  {{#if hideTitle}} <h2>Hide me!!! if you can...</h2> {{/if}}
  {{#block "content"}}{{/block}}
</div>

Implementation

{{#embed "component" hideTitle=true}}
  {{#content "content"}}<h4>This is the new way</h4>{{/content}}
{{/embed}}

Result

<div>
  <h2>I'm visible</h2>
  <h4>This is the new way</h4>
</div>

Or maybe you can pass the context??? I don't know much about Handlebars so, if this functionality already exist, can you give a example??? Thanks a lot.

shannonmoeller commented 9 years ago

The context is passed into {{#extend}} and {{#embed}}, but you may need to use ../ segments to get to the values you're looking for. That said, the ability to pass in arbitrary values via attributes, which is now supported by the baked in partials syntax ({{> partialName}}), is useful. I'll add it.

wycats/handlebars.js#182

shannonmoeller commented 9 years ago

In the meantime, this workaround should provide the functionality you're looking for in your example case.

{{!-- component --}}
<div>
    <h2>I'm visible</h2>
    {{#block "title"}}<h2>Hide me!!! if you can...</h2>{{/block}}
    {{#block "content"}}{{/block}}
</div>
{{!-- usecase --}}
{{#embed "component"}}
    {{#content "title"}}{{/content}}
    {{#content "content"}}<h4>This is the new way</h4>{{/content}}
{{/embed}}

In the usecase, defining an empty block will replace the title in the component with an empty string. You might also be able to wrap it in a conditional, but I've honestly never tried this:

{{!-- usecase --}}
{{#embed "component"}}
    {{#if ../someValue }}
        {{#content "title"}}{{/content}}
    {{/if}}
    {{#content "content"}}<h4>This is the new way</h4>{{/content}}
{{/embed}}
shannonmoeller commented 9 years ago

Feature added and released as 1.1.0.