我应该使用 jQuery 模板吗?
我有一个页面,其中搜索结果是从 XHR 响应加载的。在这种情况下使用 jQuery 模板 显示搜索结果比使用使用 .html() 或 .load() 更传统的方法?
I have a page where search results are loaded from an XHR response. What are the advantages of using jQuery templates to display search results in this situation, rather than using a more conventional approach with .html() or .load()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用模板可以让您在 Javascript 代码之外保留大量转义标记。大多数 Javascript 解决方案现在都使用该技术(据我所知,首先记录的是 John Resig</ a>):将模板标记放在
script
标记内,并将type
属性设置为text/html
(或其他内容类型浏览器无法识别text/tmpl
、text/jstmpl
,等等)。至于具体选择jQuery 模板,这是一个需要讨论的问题。有一段时间,该代码似乎将成为核心 jQuery 代码的一部分,但显然这已经不在议程上了。
(不过,还有很多替代模板库——我个人喜欢 Mustache 因为它已被移植到除 Javascript 之外的许多语言,我觉得很有用。)
Using templates allow you to keep a lot of escaped markup outside of Javascript code. Most Javascript solutions now use the technique (first documented, as far as I know, by John Resig): you put the template markup inside a
script
tag with atype
attribute set totext/html
(or some other content-type unrecognized by the browsertext/tmpl
,text/jstmpl
, whatever).As for the specific choice of jQuery templating, this is a matter of some discussion. For a while it looked like that code was going to become a part of the core jQuery code, but apparently that's off the agenda.
(There are many alternative templating libraries, though -- personally I like Mustache because it's been ported to many languages besides Javascript, which I find useful.)
主要优点是:
。javascript 模板引擎的主要缺点是执行开销,但只有当您的模板很大时,这才是问题。
希望这有帮助。
干杯
The main advantages are:
The main disadvantage of a javascript templating engine is the overhead of execution, but it's only a problem if your template is big.
Hope this helps.
Cheers