<脚本类型=“文本/模板">的说明... </script> [英] Explanation of <script type = "text/template"> ... </script>

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

问题描述

我只是偶然发现了一些我以前从未见过的东西.在 Backbone.js 的示例 TODO 应用程序的源代码中(Backbone TODO Example)他们将模板放在 <script type = "text/template"></script> 中,其中包含看起来像 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.

通过将类型设置为文本/模板",它不是浏览器可以理解的脚本,因此浏览器将简单地忽略它.这允许您将任何内容放入其中,然后可以稍后提取并由模板库使用以生成 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 不会强迫您使用任何特定的模板库 - 那里有很多:MustacheHaml生态Google Closure 模板 等(您链接到的示例中使用的是 underscore.js).这些将使用自己的语法供您在这些脚本标签中编写.

Backbone doesn't force you to use any particular templating library - there are quite a few out there: Mustache, Haml, Eco,Google Closure template, 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;脚本类型=“文本/模板"&gt;的说明... &lt;/script&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆