<script type =“ text / template”>…</ script>的说明

JavaScript

神无LEYMandy

2020-03-13

我偶然发现了以前从未见过的东西。在Backbone.js的示例TODO应用程序(Backbone TODO Example的源代码中,他们将模板<script type = "text/template"></script>包含在中,其中包含看起来像PHP之外但带有JavaScript标记的代码。

谁可以给我解释一下这个?这是合法的吗?

第1512篇《<script type =“ text / template”>…</ script>的说明》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

5个回答
神奇Green 2020.03.13

jQuery Templates is an example of something that uses this method to store HTML that will not be rendered directly (that’s the whole point) inside other HTML: http://api.jquery.com/jQuery.template/

An 2020.03.13

<script type = “text/template”> … </script> is obsolete. Use <template> tag instead.

GO 2020.03.13

这是一种将文本添加到HTML而不进行渲染或标准化的方法。

就像添加它一样没有什么不同:

 <textarea style="display:none"><span>{{name}}</span></textarea>
猿Near 2020.03.13

要添加到Box9的答案中:

Backbone.js依赖于underscore.js,后者本身实现了John Resig的原始微模板。

如果您决定将Backbone.js与Rails一起使用,请务必查看Jammit gem。它提供了一种非常干净的方法来管理模板的资产打包。 http://documentcloud.github.com/jammit/#jst

默认情况下,Jammit还使用JResig的微模板,但是它也允许您替换模板引擎。

宝儿猿 2020.03.13

通过设置type除以外的脚本标签text/javascript,浏览器将不会执行脚本标签的内部代码。这称为微型模板。此概念已在单页应用程序(又名SPA)中广泛使用。

<script type="text/template">I am a Micro template. 
  I am going to make your web page faster.</script>

对于微型模板,脚本标签的类型为text/templatejQuery创建者John Resig对此进行了很好的解释,网址为http://ejohn.org/blog/javascript-micro-templating/

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android