【发布时间】:2014-08-01 15:00:32
【问题描述】:
以下代码加载模板。
tpl = {
// Hash of preloaded templates for the app
templates: {},
// Recursively pre-load all the templates for the app.
// This implementation should be changed in a production environment. All the template files should be
// concatenated in a single file.
loadTemplates: function(names, callback) {
var that = this;
var loadTemplate = function(index) {
var name = names[index];
console.log('Loading template: ' + name);
$.get('tpl/' + name + '.html', function(data) {
that.templates[name] = data;
index++;
if (index < names.length) {
loadTemplate(index);
} else {
callback();
}
});
}
loadTemplate(0);
},
// Get template by name from hash of preloaded templates
get: function(name) {
return this.templates[name];
}
};
你可以加载像
这样的模板tpl.loadTemplates(['header', 'wine-details', 'wine-list-item'], function() {
});
但是这里的模板是在单独的 html 文件中定义的。
是否有可能我的所有模板都定义在一个 html 文件中,然后我将它们全部加载在一起。 我是否需要编写某种解析器。原因是我的主页有 40 个模板,我不想创建 40 个 Html 模板页面,而是我可以将它们保存在一个 html 页面中,而不是一起加载它们。
这可能吗?
【问题讨论】:
标签: javascript jquery templates backbone.js