Jade - 将新行转换为 <br/>并保持内容编码 [英] Jade - convert new lines to <br/> and keep the content encoded

查看:54
本文介绍了Jade - 将新行转换为 <br/>并保持内容编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Jade 模板引擎还不是很熟悉.有没有办法将 \n 等新行转换为 br 标签,同时保持其他内容编码?

例如

.replace(/\n/g,'
')

应用于编码值应该可以完成工作.但是我不确定如何对值进行编码并获得结果.有没有这方面的帮手?

解决方案

您可以使用 jades 转义方法并像这样替换返回值中的换行符:

p !{escape(foo).replace(/\n/g, '
')}

我不知道您的用例有任何内置功能.<​​/p><小时>

看起来 pug 摆脱了转义功能,所以这就是你现在必须使用的:

p !{foo.replace(/&/g, '&amp;').replace(/</g, '<').replace(/>/g, '>').replace(/\n/g, '<br/>')}

I am still not that familiar with the Jade template engine. Is there a way to convert the new lines such as \n to br tags and at the same time keep the other content encoded?

For example

.replace(/\n/g,'</br>')

applied over the encoded value should do the work. However I am not sure how to encode the value and get the result. Is there any helper for this?

解决方案

You can use jades escape method and replace the linebreaks in the return value of that like so:

p !{escape(foo).replace(/\n/g, '<br/>')}

I am not aware of any built-in functionality for your use case.


Looks like pug got rid of the escape function, so this is what you would have to use now:

p !{foo.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\n/g, '<br/>')}

这篇关于Jade - 将新行转换为 &lt;br/&gt;并保持内容编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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