Difference between revisions of "HandlebarsHelpers"
Jump to navigation
Jump to search
Line 17: | Line 17: | ||
</pre> | </pre> | ||
---- | ---- | ||
''' | '''Passing Options''' | ||
<pre> | <pre> | ||
{{changeColor "make my text red" color="blue"}} | {{changeColor "make my text red" color="blue"}} | ||
Line 23: | Line 23: | ||
'''JavaScript''' | '''JavaScript''' | ||
<pre> | <pre> | ||
Handlebars.registerHelper() | Handlebars.registerHelper("changeColor", function(text, options){ | ||
text = Handlebars.Utils.escapeExpression(text); | |||
if(options.hash.color === 'red'){ | |||
return new Handlebars.SafeString("<span class='redText'>"+ text + "</span>"); | |||
} | |||
}); | |||
</pre> | </pre> | ||
---- | ---- | ||
===[[#top|Back To Top]]< — >[[Hanglebars | Handlebars Category]]< — > [[Java Script|java Script Category]]<-->[[Main_Page| Home]]=== | ===[[#top|Back To Top]]< — >[[Hanglebars | Handlebars Category]]< — > [[Java Script|java Script Category]]<-->[[Main_Page| Home]]=== |
Revision as of 19:43, 17 January 2017
Helpers have to be defined before you call them
<script id="content" type="text/x-hanglebars-template"> {{makeLink "Text for Link" "http://www.outwater.com"}} </script>
JavaScript
Handlebars.registerHelper("makeLink", function(text, url){ text= Handlebars.Utils.escapeExpression(text); url = Handlebars.Utils.escapeExpression(url); var theLink = '<a href=" + url + '">' + text + '</a>'; return new Handlebars.SafeString(theLink); // SafeString does not escape return });
Passing Options
{{changeColor "make my text red" color="blue"}}
JavaScript
Handlebars.registerHelper("changeColor", function(text, options){ text = Handlebars.Utils.escapeExpression(text); if(options.hash.color === 'red'){ return new Handlebars.SafeString("<span class='redText'>"+ text + "</span>"); } });