gpt4 book ai didi

handlebars.js - Handlebars 模板中的多个部分

转载 作者:行者123 更新时间:2023-12-04 03:45:59 26 4
gpt4 key购买 nike

我只是对 Handlebars 作为模板解决方案有了更好的了解,并且遇到了一个我不知道如何解决的问题。
我在我的布局中添加了部分,一个用于页眉,一个用于页脚,用于从我的 View 中动态插入脚本。但是,只有第一部分会呈现。第二个(无论顺序如何)总是被省略。

我的布局是一个简单的 HTML 页面:

<!doctype html>
<html>
<head>
<title>Test site</title>
{{{_sections.head}}}
</head>
<body>
<header>
//Logo and stuff here
</header>
{{{body}}}




<script src="//code.jquery.com/jquery-2.0.2.min.js"></script>
{{{_sections.footer}}}
</body>
</html>

在我的布局文件中,我有:
{{#section 'head'}}
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.min.js"></script>
{{/section}}

//basic HTML here

{{#section ‘footer’}}
<script>
alert(“this doesn’t fire if its second!”);
</script>
{{/section}}

页眉部分出现在页面上,但页脚没有。问题是,如果我将页脚放在页面顶部(即在 {{section ‘head’}} 之前呈现,但头部部分不再呈现。

在我的 app.js 中,我正在按如下方式设置部分功能:
var handlebars = require('express3-handlebars')
.create({
defaultLayout: 'main',
helpers: {
section: function (name, options) {
if (!this._sections) {
this._sections = {};
this._sections[name] = options.fn(this);
return null;
}
}
}
});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

任何想法我在这里做错了什么或如何添加对这两个部分的支持?

谢谢

最佳答案

我认为您的部分正在相互覆盖。尝试将您的 Handlebars 创建更改为以下内容:

var handlebars = require('express3-handlebars')
.create({
defaultLayout: 'main',
helpers: {
section: function (name, options) {
if (!this._sections) {
this._sections = {};
}
this._sections[name] = options.fn(this);
return null;
}
}
});

关于handlebars.js - Handlebars 模板中的多个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24934314/

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