【问题标题】:Passing html elements into a jade file将html元素传递到jade文件中
【发布时间】:2011-11-16 04:40:33
【问题描述】:

是否可以将 HTML 元素传递到玉文件中,例如我希望以下内容用文本填充 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 文件 sn-p,每个新闻项都有一个循环

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

【问题讨论】:

    标签: javascript node.js express pug


    【解决方案1】:

    使用!{item.body} 而不是#

    【讨论】:

      【解决方案2】:

      对于@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
      

      希望这可以节省一些时间试图了解这些警告的真相!

      【讨论】:

        猜你喜欢
        • 2015-03-08
        • 2014-08-24
        • 2017-12-13
        • 1970-01-01
        • 2014-02-09
        • 2020-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多