本文介绍了&LT的说明;脚本类型="文/模板"> ...< / SCRIPT>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是偶然发现了一些我以前从未见过。在Backbone.js的的例子TODO应用程序的源代码()他们有他们的内模板一个<脚本类型=文/模板>< / SCRIPT> ,其中包含code,它看上去就像PHP,但与JavaScript代码。

I just stumbled upon something I've never seen before. In the source of Backbone.js's example TODO application (Backbone TODO Example) they had their templates inside a <script type = "text/template"></script>, which contained code that looks like something out of PHP but with JavaScript tags.

有人可以解释这样对我?这是合法的?

Can someone explain this to me? Is this legit?

推荐答案

这些脚本标签来实现模板功能(如在PHP),但在客户端的常用方法。

Those script tags are a common way to implement templating functionality (like in PHP) but on the client side.

通过设置类型为text /模板,它不是一个脚本,浏览器可以理解,所以浏览器会简单地忽略它。这允许你把有什么,然后可以在以后提取和使用一个模板库,生成HTML片段。

By setting the type to "text/template", it's not a script that the browser can understand, and so the browser will simply ignore it. This allows you to put anything in there, which can then be extracted later and used by a templating library to generate HTML snippets.

骨干不强迫你使用任何特定的模板库 - 有不少在那里:, ,,等(在本例中使用的一个你挂是的)。这些会用自己的语法,让你的脚本标签中写的。

Backbone doesn't force you to use any particular templating library - there are quite a few out there: Mustache, Haml, Eco, and so on (the one used in the example you linked to is underscore.js). These will use their own syntax for you to write within those script tags.

这篇关于&LT的说明;脚本类型=&QUOT;文/模板&QUOT;&GT; ...&LT; / SCRIPT&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 11:11