gpt4 book ai didi

node.js - 你如何在 Jade 和 Express 中使用动态布局

转载 作者:搜寻专家 更新时间:2023-11-01 00:02:38 27 4
gpt4 key购买 nike

我正在尝试对 Jade 和 Express 使用动态布局。我见过很多替代方案,但从来没有一种方法可以干净地做到这一点。

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

这是一个示例(3 种类型的模板): 模板_1 模板_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}

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

看来 Jade 都是人手编出来的。

那么,是否可以进行动态包含?还是偏音?还是动态布局?

感谢您的帮助。

最佳答案

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

您可以执行以下操作:

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

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

关于node.js - 你如何在 Jade 和 Express 中使用动态布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18017851/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com