【问题标题】:Jade/PUG JSON interpolationJade/PUG JSON 插值
【发布时间】:2016-08-07 01:15:24
【问题描述】:

首先让我告诉你,我使用的不是 Express,而是 Pug(以前称为 Jade)。

我从外部文件中读取了一个 JSON 对象。在对象内部,其中一个键具有如下所示的字符串值:

This is #[strong cool]

Jade 完全按照原样输出,但我希望对读取的字符串进行插值处理。有什么线索吗?

提前致谢!

【问题讨论】:

标签: javascript json node.js pug pugjs


【解决方案1】:

您需要使用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')

【讨论】:

  • 那也行不通。问题在于字符串在从 json 读取时被完全解释为字符串。它需要一个解析方法。我确实知道属性中的插值。谢谢!
  • 您是如何提取数据的?我在 gulp 任务中使用 locals: { 'clients': JSON.parse( fs.readFileSync('./markup/data/clients.json', { encoding: 'utf8' }) )。或者你的意思是This is strong#{JSON.stringify(cool)}
  • 嗨,Jason,我正在以与您相同的方式提取数据。但是想象一下,在您的 ./markup/data/clients.json 中,您有以下内容:{ "title": "This is #[strong cool]" }。在标题标签中输出它会像这样:h2=jsonobj.title。我希望它完全输出:<h2>This is <strong>cool</strong></h2>。在 pug 中,不抓住字符串,你会这样做:h2 This is #[strong cool] 并且请注意插值运算符与方括号是正确的。
【解决方案2】:

我的虚拟解决方案是使用 This is <strong>cool</strong>

这行得通。

【讨论】:

  • @JoãoSaro 不,这只是纯文本。
【解决方案3】:

这叫做“插值”。

这意味着“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

【讨论】:

    猜你喜欢
    • 2016-12-10
    • 2020-04-13
    • 2016-12-17
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 2012-01-22
    相关资源
    最近更新 更多