Skip to content

Instantly share code, notes, and snippets.

@AlexPashley
Last active May 25, 2020 18:39
Show Gist options
  • Save AlexPashley/5859006 to your computer and use it in GitHub Desktop.
Save AlexPashley/5859006 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