handlebars.js是否用< br>替换换行符? [英] Does handlebars.js replace newline characters with <br>?

查看:228
本文介绍了handlebars.js是否用< br>替换换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用handlebars.js进行模板化,但库似乎忽略了换行符。



处理换行符的正确方法是什么?它们是否应该在模板操作之后手动替换?解析方案

它不会自动执行,但使用帮助程序功能可以:

JS:

  Handlebars.registerHelper('breaklines ',function(text){
text = Handlebars.Utils.escapeExpression(text);
text = text.replace(/(\r\\\
| \\\
| \r)/ gm,'< br>');
返回新的Handlebars.SafeString(文本);
});

HTML模板:

 < DIV> 
{{breaklines description}}
< / div>


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?

解决方案

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>

这篇关于handlebars.js是否用&lt; br&gt;替换换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆