使用Jade创建动态链接

11

我在我的应用程序中使用Jade + Express + Node.js + Mongoose + MongoDB,但是我遇到的问题很可能是出现在Jade中:

我有一些代码如下,可以按标题、作者列出帖子列表:

div#articles
      -each post in records
         div.article
            #{post.title} was written by #{post.author}
            <a href ="#{post.title}"> Link to Article </a>

我想用Jade而不是HTML编写链接,但当我将该行替换为

a(href='#{post.title}')

它链接到/#{post.title}而不是变量名,例如/newpost1。将其更改为

a(href=#{post.title})

返回一个错误。我确定这是一个语法问题,但我在GitHub文档中找不到解决方案。

2个回答

18

我很确定你只需要这样做:

a(href=post.title)

哦,我忘了在Jade中不需要转义。谢谢! - varunsrin
在重复组内,上述方法对我无效,但是 a(href="{{post.link}}") 可以。 - Soren

5

玉:

- var records = [ { title: 'one', author: 'one' }, { title: 'two', author: 'two' } ];
div#articles
  -each post in records
     div.article
        | #{post.title} was written by #{post.author}
        a(href =post.title) Link to Article

html:

<div id="articles">
  <div class="article">one was written by one<a href="one">Link to Article</a></div>
  <div class="article">two was written by two<a href="two">Link to Article</a></div>
</div>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接