jquery 模板 - 导入文件?
我正在使用backbone.js,但据我所知,它并不关心你使用什么模板系统。目前我正在尝试 Mustache.js,但我对其他的持开放态度。不过,我对必须将模板放入字符串中的方式有点恼火:
var context = {
name: this.model.get('name'),
email: this.model.get('email')
}
var template = "<form>Name<input name='name' type='text' value='{{name}}' />Email<input name='email' type='text' value='{{email}}' /></form>";
var html = Mustache.to_html(template, context);
$(this.el).html(html);
$('#app').html(this.el);
我希望我是否可以从不同的文件或其他文件中加载它。我希望能够拥有模板文件以简化事情。例如,如果我把它全部放在一个字符串中,我就不能有中断(当然我可以有 html 中断,但这不是重点)。当队伍开始变得很长之后,就变得难以管理。
尖端?
I'm working with backbone.js, but as far as I've seen, it doesn't care what templating system you use. Currently I'm trying out mustache.js, but I'm open to other ones. I'm a little annoyed though with the way I have to put a template into a string:
var context = {
name: this.model.get('name'),
email: this.model.get('email')
}
var template = "<form>Name<input name='name' type='text' value='{{name}}' />Email<input name='email' type='text' value='{{email}}' /></form>";
var html = Mustache.to_html(template, context);
$(this.el).html(html);
$('#app').html(this.el);
I'd like if I could load it from a different file or something somehow. I want to be able to have template files in order to simplify things. For example, if I put it all in a string, I can't have breaks (well I can have html breaks, but that's not the point). After the line starts to get very long, it becomes unmanageable.
Tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更新(4/11/14):
正如下面OP的回答:
原始答案:
我刚刚用过这个几个小时前:
http://api.jquery.com/category/plugins/templates/
这是一个官方 jQuery 插件(即开发人员认可它)。
这是从字符串以外的其他内容加载模板所需的函数: http://api.jquery.com /template/
下面是在 HTML 中包含模板的代码:
它位于
标记中,因为默认情况下该标记不可见。
请注意 type="text/x-jquery-tmpl"。如果你忽略了这一点,它会尝试将其解析为 JavaScript(并且会严重失败)。
另请注意,“从不同文件加载”本质上与“读取文件”然后“从字符串加载”相同。
Updated (4/11/14):
As answered by OP below:
Original Answer:
I just used this a couple of hours ago:
http://api.jquery.com/category/plugins/templates/
It's an official jQuery plugin(i.e. the devs endorse it).
This is the function you need to use for loading templates from things other than strings: http://api.jquery.com/template/
Here's the code to have a template in HTML:
It's in a
<script>
tag, because that's not visible by default.Note the type="text/x-jquery-tmpl". If you omit that, it will try to parse it as JavaScript(and fail horribly).
Also note that "loading from a different file" is essentially the same as "reading a file" and then "loading from a string".
编辑
我刚刚找到了这个 jQuery 插件 - http://markdalgleish.com/projects/tmpload / 完全符合您的要求,并且可以与
$.tmpl
结合使用我构建了一个轻量级模板管理器,它通过 Ajax 加载模板,它允许您将模板分成更易于管理的模块。它还执行简单的内存缓存以防止不必要的 HTTP 请求。 (为了简洁起见,我在这里使用了 jQuery.ajax)
然后您可以按如下方式使用此代码,通过回调处理模板数据:
Edit
I just found this jQuery plugin - http://markdalgleish.com/projects/tmpload/ Does exactly what you want, and can be coupled with
$.tmpl
I have built a lightweight template manager that loads templates via Ajax, which allows you to separate the templates into more manageable modules. It also performs simple, in-memory caching to prevent unnecessary HTTP requests. (I have used jQuery.ajax here for brevity)
You would then use this code as follows, handling the template data via callback:
我们使用jqote2与backbone,因为它比jQuery更快,正如你所说,有很多:)
我们将所有模板放在一个tpl文件中,我们绑定到我们的template_rendered,这样我们就可以添加jquery事件等,
它们看起来像
We are using jqote2 with backbone because it's faster than jQuery's, as you say there are many :)
We have all our templates in a single tpl file, we bind to our template_rendered so we can add jquery events etc etc
They look like