简体   繁体   English

在生产代码中使用text / html javascript模板

[英]Using text/html javascript templates in production code

Has anyone implemented John Resig's micro-templating in production code? 有人在生产代码中实施过John Resig的微型模板吗? In particular I'd like to know if embedding the templates with <script type="text/html"> has caused any problems. 特别是我想知道用<script type="text/html">嵌入模板是否引起了任何问题。

A bit more information: here's the sort of thing I'm worried about: 更多信息:这是我担心的事情:

  • My users are mostly in corporate environments - could an unusual proxy server mangle or strip out the templates 我的用户主要在公司环境中-不寻常的代理服务器可能会破坏或剥离模板
  • The embedded templates seem to fail W3C validation. 嵌入式模板似乎无法通过W3C验证。 What if, for instance, the next version of IE decides it doesn't like them? 例如,如果下一版IE决定不喜欢它们怎么办?

While I haven't used @jeresig's micro-templating, I did roll my own which uses &lt;script type="text/html"&gt; 虽然我没有使用@jeresig的微型模板,但确实使用了&lt;script type="text/html"&gt; . I've used it in a couple of (albeit basic) production sites without problems. 我已经在几个(尽管是基本的)生产站点中使用了它,没有出现问题。 The HTML5 spec itself refers to something similar (near the end of the section I've linked to). HTML5规范本身引用了类似的内容(在我链接到的部分的末尾附近)。 AFAIK the block only executes when the MIME type is a recognized script type, otherwise the block is just part of the document. AFAIK该块仅在MIME类型是可识别的脚本类型时才执行,否则,该块只是文档的一部分。

I abandoned inlining templates through scripttags as my view layer would create redundant duplicate script templates. 我通过脚本标签放弃了内联模板,因为我的视图层将创建冗余的重复脚本模板。 Instead I placed the templates back into the JavaScript as string: 相反,我将模板作为字符串放回JavaScript中:

var tpl = ''.concat(
    '<div class="person">',
        '<span class="name">${name}</span>',
        '<span class="lastname">${lastName}</span>',
    '</div>'
);

I used the string concat trick so I could make the template string readable. 我使用了字符串concat技巧,因此可以使模板字符串可读。 There are variations to the trick like an array join or simply additive concatenation. 技巧有很多变化,例如数组联接或简单的加法级联。 In any case, inline script templates works and works well, but in a php/jsp/asp view layer, chances are you will create redundant duplicate script templates unless you do even more work to avoid it. 在任何情况下,内联脚本模板都可以正常工作,但是在php / jsp / asp视图层中,除非您要做更多的工作来避免它,否则您很有可能会创建冗余的重复脚本模板。

Furthermore, these templates become rather complex the more logic you have to add to them, so I looked further and found mustache.j s which imo. 此外,您必须向其中添加更多逻辑,因此这些模板变得相当复杂,因此我进一步看了看,发现mustache.j属于哪个imo。 is far superior and keeps the logic (conditions and dynamic variable definitions) in the JavaScript scope. 的优势更强,并将逻辑(条件和动态变量定义)保留在JavaScript范围内。

Another option would be to retreive template strings through ajax, in which case you can put each template inside it's own file and simply grant it a .tpl extention. 另一个选择是通过ajax检索模板字符串,在这种情况下,您可以将每个模板放入其自己的文件中,并简单地为其授予.tpl扩展名。 The only thing you have to worry about is the http request roundtrip, which should not take too long for small .tpl files and is imo. 您唯一需要担心的是http请求往返,对于较小的.tpl文件,它应该不会花费太长时间,并且是imo。 insignificant enough. 无关紧要的。

I have actually, and it works great. 我确实有,而且效果很好。 Though only for webkit powered browsers so can't vouch for others. 尽管仅适用于基于Webkit的浏览器,所以不能担保其他浏览器。 But what problems are you expecting? 但是您期望什么问题呢? The approach is simple and I can't think of how it might break. 这种方法很简单,我想不出它怎么破。

If you're using jQuery I can recommend jQuery templates instead: 如果您使用的是jQuery,我可以推荐jQuery模板:

http://github.com/nje/jquery-tmpl http://github.com/nje/jquery-tmpl

I think it's supposed to be introduced officially in jQuery 1.5 我认为应该在jQuery 1.5中正式引入

BTW. BTW。 I think both Resigs script and jQuery templates relies on using innerHTML , so as long as that works in your browser you should be OK. 我认为Resigs脚本和jQuery模板都依赖于使用innerHTML ,因此只要在您的浏览器中都可以,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM