Skip to content

Instantly share code, notes, and snippets.

@rpocklin
Last active December 27, 2015 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpocklin/7329240 to your computer and use it in GitHub Desktop.
Save rpocklin/7329240 to your computer and use it in GitHub Desktop.
Some HB handlers which extend template functions.
/* assume you have a HB template compiled called 'question_btn'
which contains {{outlet}} within it as optional embedded content
# example of question_btn.hbs
<a target="_blank" href="{{question_url}}">
<button type="button" class="pull-right btn question" title="ask seller a question">
{{#if outlet}}{{outlet}}{{else}}<i class="icon-question-sign"></i>{{/if}}
</button>
</a>
// Usage
{{include 'question_btn' this}}
{{#layout 'question_btn' this}}
<i class="icon-user"></i>
{{/layout}}
*/
/* you can change this to lazy-load/recompile your templates if you are in DEVELOPMENT mode,
assumes you are pre-compiling these templates in PRODUCTION mode */
var build_template = function(template_name, context) {
return new Handlebars.SafeString(HandlebarsTemplates[template_name](context));
};
// includes a snippet without any embedded content
Handlebars.registerHelper('include', function(template_name, context) {
return build_template(template_name, context);
});
// like include but as a block helper, you can pass HTML within the templates {{outlet}}
Handlebars.registerHelper('layout', function (template_name, context, options) {
context = _.extend({outlet: new Handlebars.SafeString(options.fn(this))}, options);
return build_template(template_name, context);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment