你如何使用Jade和Express的动态布局

时间:2013-08-02 13:17:03

标签: node.js express coffeescript haml pug

我正在尝试使用Jade和Express的动态布局。我已经看到了很多替代方案,但从来没有办法干净利落地做到这一点。

我的应用程序将有多种模板,包括dynamiccaly其他模板。它就像我的应用程序基石一样,所以如果没有它我就无法走得更远......

以下是一个示例(3种模板):   的 template_1   的 template_2   的 template_3

template_1 包括 template_2 和另一个 template_3

所以,如果它是静态的,我会这样做:

# template.coffee
exports.index = (req, res) ->
  res.render 'template_1'

# template 1
Some HAML content

block content
  div.block
    include template_2
  div.block
    include template_3

但是,我想通过局部变量提供模板列表:

所以,我想做这样的事情

# template.coffee
exports.index = (req, res) ->
  res.render 'template_1', {
    template_list: [
      'template_2',
      'template_3'
    ]
  }

# template 1
Some HAML content

block content
  - each current_template in template_list
    div.block
      include current_template

# template 1
Some HAML content

block content
  - each current_template in template
    div.block
      include #{current_template}

但它不起作用。它将 include extends 之后的任何内容作为字符串...

似乎玉手工编制。

那么,是否有可能进行动态包含?还是局部的?还是动态布局?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

如果您还在使用browserify,请考虑使用jadeify:https://github.com/domenic/jadeify在客户端进行渲染。

您可以执行以下操作:

var template = require("./template.jade");

document.getElementById("my-thing").innerHTML = template({
    localVar: "value",
    anotherOne: "another value"
});