Skip to content

Instantly share code, notes, and snippets.

@mcky
Last active August 29, 2015 14:16
Show Gist options
  • Save mcky/338847ddb8ffe97f98bc to your computer and use it in GitHub Desktop.
Save mcky/338847ddb8ffe97f98bc to your computer and use it in GitHub Desktop.

Block helper for hbs.

First argument should match the filename of the component (e.g. modal). Anything passed to the component as an attribute will be added to the args object. Content can include any HTML/CSS/JS and even other parials.

Note: Uncomment 'data' in the helper to get access to locals within the component. Otherwise pass any locals to the component as attributes.

hbs.registerHelper('component', function(component, options) {
var componentPath = __dirname + '/views/partials/blocks/'+ component +'.html'
, template = hbs.compile(fs.readFileSync(componentPath, 'utf8'))
, content = new hbs.handlebars.SafeString(options.fn(this))
return new hbs.handlebars.SafeString(template({
content: content
, args: options.hash
// , data: options.data.root
}))
})
{{#component 'modal' title='Login'}}
<!-- Any content, even other partials -->
{{>loginForm }}
{{/component}}
<div class="modal modal--active">
<div class="modal__center">
<div class="modal__header">{{args.title}}</div>
<div class="modal__content">
{{ content }}
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment