Express.js自定义模板引擎(板) [英] Express.js custom template engine (plate)

查看:131
本文介绍了Express.js自定义模板引擎(板)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图让板模板引擎与express.js一起工作。我最初的尝试是这样的:

I am trying to get plate template engine to work with express.js. My initial attempt was this:

app.register('.html', {
    compile: function (str, options) {
        var template = new plate.Template(str);
        return function(locals) {
            return template.render(locals, function(err, data) {
                return data;
            });
        }
    }
});

我看到问题是template.render不返回任何东西(未定义),但传递数据到回调。我不知道如何在这种情况下使它工作,因为Express希望编译函数返回一个直接返回渲染模板的函数。

I see that the problem is that template.render doesn't return anything (undefined) but passes the data to a callback. I'm not sure how to make it work in this case as Express expects the compile function to return a function that directly returns a rendered template when called.

我在想也许我可以使用承诺来解决这个问题,但是在这里没有成功,因为明确的代码并不期望得到回报。我不能太高于承诺,所以我可能只是做错了:

I was thinking perhaps I can use promises to solve this issue but had no success there either since the express code doesn't expect a promise to be returned. Im not too up to speed on promises so I may just be doing it wrong:

app.register('.html', {
    compile: function (str, options) {
        var promise = new Promise();
        var template = new plate.Template(str);
        return function(locals) {
            template.render(locals, function(err, data) {
                promise.resolve(data);
            });
            return promise;
        }
    }
});

这是一个可以实现的自定义实现的示例。不同的是,下划线模板template()函数直接返回呈现的字符串,如下所示:

Here is an example of a custom implementation that does works. The difference is that underscore templates template() function directly returns the rendered string like so:

app.register('.html', {
    compile: function (str, options) {
        var template = _.template(str);
        return function (locals) {
            return template(locals);
        };
    }
});

我真的很喜欢使用Plate模板,因为{%block%}标签太棒了。任何帮助是赞赏。

I'd really like to use Plate templates since the {% block %} tag is so awesome. Any help is appreciated.

相关文档:

plate's github docs

express.js app.register docs

推荐答案

板的创建者迅速添加补丁到项目,使其兼容快递后我问这个。 plate@0.0.13+有更改,您可以看到实施细节这里

The creator of plate promptly added a patch to the project to make it compatible with express after I asked this. plate@0.0.13+ has the change and you can see implementation details here

这篇关于Express.js自定义模板引擎(板)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆