Backbonejs有胡子的模板。 [英] Backbonejs with mustache template.
问题描述
我想这样做与使用模板的小胡子一个backbonejs简单的应用程序。你能给我一个示例程序??
I want to do a simple application using backbonejs with mustache template. Can you give me a sample program??
新节点的文件:
var Person = Backbone.Model.extend({
defaults: {
name: 'Guest Worker',
}
});
var PersonView = Backbone.View.extend({
tagName: 'li',
initialize: function(){
_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g
};
this.render();
},
render: function(){
var template1 = _.template("Hello {{ name }}!");
this.$el.html( this.template1(this.model.toJSON()));
}
});
这是我的js code。
This is my js code.
推荐答案
胡子模板引擎不会以这种方式工作。下面是从文档的一个小例子:
Mustache template engine doesn't work this way. Here's a small example from the documentation :
var view = {
title: "Joe",
calc: function () {
return 2 + 4;
}
};
// output will then contain processed html
var output = Mustache.render("{{title}} spends {{calc}}", view);
无论如何,我用把手会建议你( http://handlebarsjs.com/ ),而不是胡子。这几乎相同的语法(它有谐音的胡子一样),但更强大得益于其助手。
Anyway, i would recommend you using Handlebars (http://handlebarsjs.com/) instead of Mustache. It's almost the same syntax (and it has partials as Mustache does), but far more powerful thanks to its helpers.
最后,你应该使用的东西precompile你的模板。您可以使用车把的一( http://handlebarsjs.com/$p$pcompilation.html)或其他人喜欢早午餐,或者咕噜。
Finally, you should use something to precompile your templates. You can either use handlebars's one (http://handlebarsjs.com/precompilation.html) or another one like Brunch, or Grunt.
确定,让我们尝试详细一点...我不会给你任何完整的例子(我没有一个正确的,现在,它不会教你任何东西),但一个我上面贴应足以理解胡子基础。
现在,你必须找到一种方法,precompile你的模板,这里的一些线索答案:<一href=\"http://stackoverflow.com/questions/14601578/how-to-load-templates-with-hogan-js-from-an-external-file\">How从外部文件中加载具有Hogan.JS模板?
OK, let's try to elaborate a bit... I won't give you any complete example (i don't have one right now, and it wouldn't teach you anything), but the one i posted above should be sufficient to understand Mustache basics. Now you have to find a way to precompile your templates, here's an answer with some clues : How to load templates with Hogan.JS from an external file?
这篇关于Backbonejs有胡子的模板。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!