如何在Can.js中注册预编译模板

How do you register precompiled templates in Can.js

本文关键字:编译 注册 Can js      更新时间:2023-09-26

我正在使用can.js并尝试预编译和加载模板。我已经采用了我的模板并使用can-compile对其进行了预编译,然后将它们加载到生成的脚本文件中,如下所示:

(function(window) {
    can.Mustache('block_tpl.mustache', '<template string goes here>');
}(this));

我不认为你通过'can'注册它。小胡子'?有谁知道如何使用can.js注册预编译模板?

编辑:

@Daff,当我使用罐头时。胡子(我正在使用can.jquery,顺便说一句)我发现(a)can.mustache是未定义的,并且(b)例如,如果我输入:

can.Mustache('jim_tpl', "<p>My name is Jim</p>");

。当它到达胡须构造函数时:

var Mustache = function(options, helpers) {
            if (this.constructor !== Mustache) {
                var mustache = new Mustache(options);
                return function(data, options) {
                    return mustache.render(data, options);
                };
            }
            if (typeof options === "function") {
                this.template = {fn: options};
                return;
            }
            can.extend(this, options);
            this.template = this.scanner.scan(this.text, this.name);
        };

。在构造新的胡须(ln. 3)的地方,它将模板名称作为选项参数传递。当您将其传递给底部"选项"中的扫描仪 fn 时,模板名称字符串现在变成了一个对象,每个属性都是该字符串的一个字母......

我们仍在使用 CanJS 2.0.7,我们使用 can.view.mustache() 预编译我们的模板

请参阅:https://github.com/reciprocity/ggrc-core/blob/develop/src/ggrc/assets/javascripts/application.js#L1490

这就是您预注册视图的方式,如文档所述:

如果传递了两个参数,则第一个参数是将注册到 can.view 的模板的 id。

您可以使用 can.mustache 和大写can.Mustache 。您是否遇到任何问题?