Skip to content

Instantly share code, notes, and snippets.

@minwe
Forked from AlexPashley/helpers.js
Created June 8, 2017 11:39
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 minwe/c39a6f88db46e3b6b01e4f999f753b7f to your computer and use it in GitHub Desktop.
Save minwe/c39a6f88db46e3b6b01e4f999f753b7f to your computer and use it in GitHub Desktop.
JS: Handlebars - Block Helpers #1 nl2br - Replace returns with <br> #2 If Greater than comparison operator #3 Find and replace function
// {{#nl2br}} replace returns with <br>
Handlebars.registerHelper('nl2br', function(options) {
var nl2br = (options.fn(this) + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + '<br>' + '$2');
return new Handlebars.SafeString(nl2br);
});
/**
* {{#ifGt}} greater than helper
*
* @param1 int param1
* @param1 int param2
* @return boolean
*/
Handlebars.registerHelper('ifGt', function(param1, param2, options) {
if( param1 > param2 ) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
/**
* {{#replace}} replace specified content
*
* @param string find
* @param string replace
* @return string
*/
Handlebars.registerHelper('replace', function( find, replace, options) {
var string = options.fn(this);
return string.replace( find, replace );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment