Skip to content

Instantly share code, notes, and snippets.

@jonschlinkert
Last active February 3, 2018 05:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonschlinkert/1894cc96628627aa5564 to your computer and use it in GitHub Desktop.
Save jonschlinkert/1894cc96628627aa5564 to your computer and use it in GitHub Desktop.

br helper

usage

{{#br [number of breaks]}}
// content
{{/br}}

The helper

Handlebars.registerHelper('br', function(n, options) {
  options = options || {};
  var ele = '';
  var count = n - 1;
  for (var i = 0; i <= count; i++) {
    ele += '<br>';
  }
  var res = options.fn(this).replace(/[\r?\n?]+/g, ele);
  return new Handlebars.SafeString(res);
});

Examples

{{#br 2}}
content
here
{{/br}}

Results in

content<br><br>
here

Or a single newline

{{#br 1}}
content
here
{{/br}}

Results in

content<br>
here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment