在HTML属性中插值-Pug(Jade)

问题描述:

我正在尝试在Jade中使用动态的 href 属性构造一个 anchor 标记.

I am trying to construct an anchor tag with a dynamic href attribute in Jade.

我确实查看了文档和一些SO问题,但它们没有帮助我.这就是我尝试过的.

I did go through the docs and some SO questions but they didn't help me. This is what I tried.

a(href= "http://www.imdb.com/title/#{movie.imdb_id}") Know more

但是它呈现

http://www.imdb.com/title/#{movie.imdb_id}  

而不是

http://www.imdb.com/title/tt1234567

但这可行

a(href= "http://www.imdb.com/title/" + movie.imdb_id) Know more

也是.

- var url = "http://www.imdb.com/title/" + movie.imdb_id;
  a(href= url) Know more

第一个版本出了什么问题?

What's wrong with the first version?

插值仅在文本中可用.

您需要对属性使用JS字符串串​​联:

You need to use JS string concatenation for attributes:

a(href="http://www.imdb.com/title/" + movie.imdb_id) Know more

如果您的JavaScript运行时支持 ES2015模板字符串,您也可以使用它们(注意反引号):

If you JavaScript runtime supports ES2015 template string, you can also use them (notice the backticks):

a(href=`http://www.imdb.com/title/${movie.imdb_id}`) Know more

参考