将html元素传递到jade文件中

时间:2011-09-13 13:33:06

标签: javascript node.js express pug

是否可以将HTML元素传递到jade文件中,例如我希望以下内容用文本填充p元素,并在p元素中嵌入一些包含文本的代码元素。

包含字符串的JSON

var news = {
  one : {
    title : "Using JSON",
     body : "Using JSON is a great way of passing information into the jade template files."+
            "It can be looped over using jades each syntax <code>test</code>"
  },
  two : {
    title : "",
    body : ""
  }
}

路由HTTP请求

// Routes
app.get(navigation.home.uri, function(req, res){ //Home
  res.render(navigation.home.url, {
    title: navigation.home.title,
    navigation: navigation,
    news: news
  });
});

Jade文件摘要,每个新闻项都有一个循环

  section#news
    - each item in news
      article(class="news")
        header
          h2 #{item.title}
        p #{item.body}

2 个答案:

答案 0 :(得分:14)

使用!{item.body}代替#

答案 1 :(得分:1)

对于@Jack给出的示例,接受的答案确实是正确的,但我使用了这个没有初始p标记:

block content
    .jumbotron
        !{template.sections.welcome}
    .panel.panel-default
        !{template.sections.headline}

这是不正确的,会抛出一些像

这样的Jade警告
Warning: missing space before text for line 6 of jade file "~/demo/views/home.jade"

这是因为它不是使用Unescaped Buffered Code而是使用插值,而是用于长字符串。

所以正确的语法(如here所述)将是:

block content
    .jumbotron
        != template.sections.welcome
    .panel.panel-default
        != template.sections.headline

希望这可以节省一些时间试图找到这些警告的底部!

相关问题