Meteor: How to render line breaks?

后端 未结 5 534
滥情空心
滥情空心 2020-12-11 05:30

I have the following variable: \"hello↵↵world\". (the value is coming from a textarea)

The problem is when I ask Meteor to display it with {{content}}, everything ap

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-11 06:17

     worked fine for me until I had long lines of text. By default it disables line wrapping and long lines break the page layout or are cut off.

    You could probably get around it with white-space: pre-wrap; but what I ended up doing was create a Spacebars-Helper that escapes the text first and then replaces all breaks with

    UI.registerHelper('breaklines', function(text, options) {
      text = s.escapeHTML(text);
      text = text.replace(/(\r\n|\n|\r)/gm, '
    '); return new Spacebars.SafeString(text); });

    I would then use the helper in my templates in the following way:

    {{breaklines title}}
    

    escapeHTML is part of Underscore.string, a collection of string manipulation extensions that you can pull in with meteor add underscorestring:underscore.string, but any other way of escaping html should work fine as well.

提交回复
热议问题