Does handlebars.js replace newline characters with
?

前端 未结 6 2095
星月不相逢
星月不相逢 2020-12-24 11:45

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

6条回答
  •  一生所求
    2020-12-24 11:59

    I've used the code posted by @Uri and it was very useful.

    But I realised that when I register the helper, the function parameter that it receives is not the text, but the function that is called inside the Handlebars template. So first I had to call it to get the text.

    In order to clarify, I had to do:

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

    This is the only way I could make it work.

提交回复
热议问题