【问题标题】:How to make so that Jade (Node.js) doesn't close the <body> tag如何使 Jade (Node.js) 不会关闭 <body> 标签
【发布时间】:2013-10-02 07:04:37
【问题描述】:

我正在尝试向客户端发送多个数据块,每个数据块都由 Express Node.js 框架中的 Jade 模板引擎呈现。

  1. 我有几个视图,例如 header、viewA、viewB、viewC 等。
  2. 对于每个请求,我都需要呈现标题部分视图并将其作为一个块发送,以便客户端浏览器开始呈现它。呈现标题视图时,我不希望关闭内部的 &lt;body&gt; 标记,因为会有更多数据出现在 &lt;body&gt; 标记内。
  3. 与此同时,我需要进行一些计算,然后渲染另一个视图:A、B 或 C。
  4. 一旦发送 A、B 或 C 视图,我将关闭响应,这意味着关闭 &lt;/body&gt;&lt;/html&gt; 标记。

听起来很简单。但问题是Jade在渲染header view时关闭了&lt;html&gt;&lt;body&gt;标签。

我知道如何使用本机 Node.js 响应对象手动执行此操作,但是不知道如何以正确的方式使用 Jade 执行此操作。

我目前看到的唯一解决方案是手动将标题部分向下发送以打开 &lt;body&gt; 标签,然后通过 res.partial() 将其余部分渲染为 Jade 部分。

非常感谢任何提示。

【问题讨论】:

    标签: node.js pug template-engine


    【解决方案1】:

    尽管我可以接受上述似乎有效但没有奏效的答案。不过,以下代码确实有效。

    !!! 5
    <html>
    head
        meta(http-equiv="Content-Type",content="text/html;charset=utf-8")
        meta(http-equiv="Cache-Control", content="no-cache")
        meta(http-equiv="Expires", content="0")
        title= title
        link(rel="shortcut icon",href='/img/nelo2_logo.ico')
        link(rel='stylesheet',href='/css/bootstrap.css')
        link(rel='stylesheet',href='/css/jquery-ui.css')
        link(rel='stylesheet',href='/css/chosen.css')
        link(rel='stylesheet',href='/css/common.css')
        link(rel='stylesheet',href='/css/style.css')
        script(src='/js/core/require.js')
    <body>
    

    因此解决方案不是缩进&lt;html&gt;&lt;body&gt; 标记,而是将它们放在同一列上。这样 Jade 就不会关闭它们。

    【讨论】:

      【解决方案2】:

      您可以使用!=输出任何文本(包括原始html)

      != "<body>"
        some
           more
              tree(attr=1)
      

      这个输出的结果:

      <body>
      <some>
        <more>
          <tree attr="1"></tree>
        </more>
      </some>
      

      【讨论】:

      • 这应该在哪个版本的 Jade 中工作?我有0.26.3,上面的语法不起作用:编译错误。
      • 对,我就是这么做的。请添加最低版本要求,我会接受您的回答。谢谢!
      • 它在文档中至少存在 2 年 - github.com/visionmedia/jade/commit/… 。 (v0.22.1 应该够用了)你确定你有超过 2 年的版本吗?
      猜你喜欢
      • 2023-04-01
      • 2017-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多