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?
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.
HTML EscapingHandlebars will not escape a Handlebars.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With