Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does handlebars.js replace newline characters with <br>?

Trying to use handlebars.js for templating but the library seems to ignore newlines.

What is the correct way to deal with newlines? Should they be replaced manually after the templating action?

like image 807
Uri Avatar asked Sep 08 '12 13:09

Uri


People also ask

What does handlebars do in JS?

Handlebars. js is a Javascript library used to create reusable webpage templates. The templates are combination of HTML, text, and expressions. The expressions are included in the html document and surrounded by double curly braces.

Does handlebars escape HTML?

HTML EscapingHandlebars will not escape a Handlebars.


1 Answers

It doesn't do so automatically, but using the helpers feature this can be achieved:

JS:

Handlebars.registerHelper('breaklines', function(text) {     text = Handlebars.Utils.escapeExpression(text);     text = text.replace(/(\r\n|\n|\r)/gm, '<br>');     return new Handlebars.SafeString(text); }); 

HTML template:

<div>     {{breaklines description}} </div> 
like image 74
Uri Avatar answered Sep 24 '22 00:09

Uri