【发布时间】:2016-08-07 01:15:24
【问题描述】:
首先让我告诉你,我使用的不是 Express,而是 Pug(以前称为 Jade)。
我从外部文件中读取了一个 JSON 对象。在对象内部,其中一个键具有如下所示的字符串值:
This is #[strong cool]
Jade 完全按照原样输出,但我希望对读取的字符串进行插值处理。有什么线索吗?
提前致谢!
【问题讨论】:
标签: javascript json node.js pug pugjs
首先让我告诉你,我使用的不是 Express,而是 Pug(以前称为 Jade)。
我从外部文件中读取了一个 JSON 对象。在对象内部,其中一个键具有如下所示的字符串值:
This is #[strong cool]
Jade 完全按照原样输出,但我希望对读取的字符串进行插值处理。有什么线索吗?
提前致谢!
【问题讨论】:
标签: javascript json node.js pug pugjs
您需要使用This is #{strong cool}。 Note the curly brackets.
如果您在文档中遗漏了它,Pug changed how interpolation works for attributes
曾经是
a(href="#{link}")
a(href='before#{link}after')
但现在你应该使用
a(href=link)
a(href=`before${link}after\`)
a(href='before' + link + 'after')
【讨论】:
locals: { 'clients': JSON.parse( fs.readFileSync('./markup/data/clients.json', { encoding: 'utf8' }) )。或者你的意思是This is strong#{JSON.stringify(cool)}?
{ "title": "This is #[strong cool]" }。在标题标签中输出它会像这样:h2=jsonobj.title。我希望它完全输出:<h2>This is <strong>cool</strong></h2>。在 pug 中,不抓住字符串,你会这样做:h2 This is #[strong cool] 并且请注意插值运算符与方括号是正确的。
我的虚拟解决方案是使用
This is <strong>cool</strong>
这行得通。
【讨论】:
这叫做“插值”。
这意味着“messages()”被转义,如果你有以下代码,则为 fx:
var randomText = <p>this is a text</p>
p=随机文本 通常,未转义,会产生实际的字符串:
<p> this is a text</p>
但如果我输入这个:
p!= randomText
它实际上会变成一个 p 标签,看起来就像这样:
this is a text
希望对你有帮助:-)
您可以在此处阅读更多关于文档的信息:https://pugjs.org/language/interpolation.html
【讨论】: