Handlebars模板中的多个部分

LDJ*_*LDJ 4 handlebars.js

我只是更了解Handlebars作为一个模板解决方案,并遇到了一个我不知道如何解决的问题.我已经在我的布局中添加了一些部分,一个用于标题,另一个用于页脚,用于从我的视图中动态插入脚本.但是,只有第一部分呈现.总是省略第二个(不管顺序).

我的布局是一个简单的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>
Run Code Online (Sandbox Code Playgroud)

在我的布局文件中,我有:

{{#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}}
Run Code Online (Sandbox Code Playgroud)

页眉部分显示在页面上,但页脚没有.问题是,如果我将页脚放在页面的顶部(即在{{section'head'}}之前,然后呈现但是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');
Run Code Online (Sandbox Code Playgroud)

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

谢谢

小智 5

我认为你的部分会相互覆盖.尝试将把手更改为以下内容:

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;
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)