Last active
December 27, 2015 12:58
-
-
Save rpocklin/7329240 to your computer and use it in GitHub Desktop.
Some HB handlers which extend template functions.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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