首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过Handlebars.precompile使用模板(vs CLI)

如何通过Handlebars.precompile使用模板(vs CLI)
EN

Stack Overflow用户
提问于 2014-05-29 18:49:18
回答 1查看 974关注 0票数 3

我所处的.NET环境中,我们不能使用Node.js (需求,愚蠢的事情)。我们有一个自定义的主干应用程序,并且使用工具栏作为客户端模板。由于该项目的限制,我们使用Handlebars.precompile手动预编译所有模板。我做了一些google操作,并在Stackoverflow中搜索过,但是在Handlebars.precompile上没有看到多少有用的文档(它的输出与使用CLI工具不同)。我的问题是:一旦我以编程方式预编译了车把模板,如何使用它们?此实现不将它们添加到Handlebars.templates命名空间中。

例如,我们将使用一个基本模板(称为Stuff.handlebars):

代码语言:javascript
复制
{{!-- begin template --}}
    <strong> {{test}} </strong>
{{!-- end template --}}

下面是使用Handlebars.precompile的输出

代码语言:javascript
复制
function (Handlebars, depth0, helpers, partials, data) {
    this.compilerInfo = [4, '>= 1.0.0'];
    helpers = this.merge(helpers, Handlebars.helpers);
    data = data || {};
    var buffer = "",
        stack1, helper, functionType = "function",
        escapeExpression = this.escapeExpression;


    buffer += "<strong> ";
    if (helper = helpers.test) {
        stack1 = helper.call(depth0, {
            hash: {},
            data: data
        });
    } else {
        helper = (depth0 && depth0.test);
        stack1 = typeof helper === functionType ? helper.call(depth0, {
            hash: {},
            data: data
        }) : helper;
    }
    buffer += escapeExpression(stack1) + " </strong>";
    return buffer;
}

当通过node.js工具栏编译器(handlebars stuff.handlebars -f test.js)运行时,输出如下:

代码语言:javascript
复制
(function () {
    var template = Handlebars.template,
        templates = Handlebars.templates = Handlebars.templates || {};
    templates['stuff'] = template({
        "compiler": [5, ">= 2.0.0"],
        "main": function (depth0, helpers, partials, data) {
            var helper, functionType = "function",
                escapeExpression = this.escapeExpression;
            return "\r\n    <strong> " + escapeExpression(((helper = helpers.test || (depth0 && depth0.test)), (typeof helper === functionType ? helper.call(depth0, {
                "name": "test",
                "hash": {},
                "data": data
            }) : helper))) + " </strong>\r\n";
        },
        "useData": true
    });
})();

我的问题是:如何适当地使用以编程方式创建的工具栏模板并将数据传递给它们?我目前正在将它们命名为App.Templates.mytemplatename = ...output of handlebars.precompile(source)...

例如,为了设置一个模板,我是:

代码语言:javascript
复制
var foo = $("#foo");
$(foo).html(Handlebars.template(App.Templates.Stuff));

这将正确地输出减去数据。如何将数据传递到其中?

请记住,我只是在试图传递数据的页面上使用handlebars.runtime.js库(否则,我不会执行所有这些步骤)。

编辑:好的,我找到答案了。

使用上面的示例,我创建了一个名为“testObj”的对象:

代码语言:javascript
复制
testObj = { test: "foo bar" };

接下来,我将Handlebars.template()调用分配给一个变量:

代码语言:javascript
复制
var template = Handlebars.template(App.Template.Stuff);

最后,我将对象作为参数传入:

代码语言:javascript
复制
template(testObj);

其输出为:"foo bar"

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-29 19:32:18

使用上面的示例,我创建了一个名为“testObj”的对象:

代码语言:javascript
复制
testObj = { test: "foo bar" };

接下来,我将Handlebars.template()调用分配给一个变量:

代码语言:javascript
复制
var template = Handlebars.template(App.Template.Stuff);

最后,我将对象作为参数传入:

代码语言:javascript
复制
template(testObj);

其输出为:"foo bar"

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23940767

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档