Pug / Jade插值不适用于属性

问题描述 投票:0回答:2

我有一个这样渲染的页面:

res.render('account.pug', {user: req.user._id})

-并且应该显示与用户相对应的个人资料图片,如下所示:

img(id='profilepicture' src='profilepictures/#{user}')

但是,哈巴狗渲染为:

<img id='profilepicture' src='profilepictures/#{users}' />

—代替:

<img id='profilepicture' src='profilepictures/USERID' />

-因此不会显示正确的个人资料照片。

这很奇怪,因为当我编写div #{user}时,它正确地呈现为<div>USERID</div>,因此它显然与我在属性中间字符串上进行插值有关。我曾尝试使用反勾而不是引号,但这也不起作用。

node.js pug
2个回答
0
投票

尝试一下

img#profilepicture(src='profilepictures/' + users)

文档:https://pugjs.org/language/attributes.html

有用的站点:https://pughtml.com


0
投票

作为noted in the Pug documentation,属性中不再支持#{foo}插值语法。

代替使用—

img#profilepicture(src='profilepictures/#{user}') // no longer supported

-您可以使用(如@Shinigami指出::

img#profilepicture(src='profilepictures/' + user) // supported

或者,如果您具有ES6支持,则可以使用template strings

img#profilepicture(src=`profilepictures/${user}`) // supported

((请注意,最后一个使用${}而不是#{}

© www.soinside.com 2019 - 2024. All rights reserved.