Express 在Jade中,为什么有时我可以按原样使用变量,而在其他时候必须将它们括在#{……}中?

Express 在Jade中,为什么有时我可以按原样使用变量,而在其他时候必须将它们括在#{……}中?,express,pug,Express,Pug,请看下面的代码 for story in book if story.title.length < 140 - var storyTitle = story.title; else - var storyTitle = story.title.substring(0, 140) + '...'; .tiles

请看下面的代码

        for story in book 
            if story.title.length < 140
                - var storyTitle = story.title;
            else
                - var storyTitle = story.title.substring(0, 140) + '...';

            .tiles
                a(href= 'http://' + story.link)
                    img(src=story.thumbnail, width='150', height='150')
                p Title: #{storyTitle}
                p Date: #{story.time}
                p Author: #{story.authorName}
如果没有小胡子,它只需在浏览器中打印字符串“Title:storyTitle”

另一个例子是,如果我使用
img(src={story.thumbnail},width='150',height='150')
,它不起作用,我在浏览器中得到一个html字符串(%20%20…某物…)


那么是什么带来的呢?

区别在于标签的内容和属性。在属性中,可以使用不带大括号的变量,如

img(src=story.thumbnail
因为不能只将文本放在属性值中,所以它必须是字符串:

img(src="/images/story.jpg")
你不能这么做

img(src=/images/story.jpg)
但是在标记的内容中,您必须使用哈希+大括号
{}
,以便Jade知道哪些位是变量,哪些位只是文本

如果要在标记属性中使用哈希+大括号,可以这样做:

img(src="#{story.thumbnail}")
简单地说

等号
(=)
之后和代码块内部没有花括号。其他地方使用
{}


非常感谢。这非常有帮助。注意:较新版本的Jade(重命名为Pug)在内嵌变量时使用了稍微不同的语法。有关更新的语法,请参阅。
img(src="#{story.thumbnail}")