博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
<script type =“text / template”> ... </ script>的说明
阅读量:2290 次
发布时间:2019-05-09

本文共 2701 字,大约阅读时间需要 9 分钟。

本文翻译自:

I just stumbled upon something I've never seen before. 我偶然发现了一些我以前从未见过的东西。 In the source of Backbone.js's example TODO application ( ) 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. 在Backbone.js的示例TODO应用程序( )的源代码中,他们的模板位于<script type = "text/template"></script> ,其中包含的代码看起来像是PHP之外但带有JavaScript标记的代码。

Can someone explain this to me? 谁可以给我解释一下这个? Is this legit? 这是合法的吗?


#1楼

参考:


#2楼

It's legit and very handy! 这是合法的,非常方便!

Try this: 试试这个:

Several Javascript templating libraries use this technique. 一些Javascript模板库使用这种技术。 is a good example. 就是一个很好的例子。


#3楼

By setting script tag type other than text/javascript , browser will not execute the internal code of script tag. 通过设置text/javascript以外的脚本标签type ,浏览器不会执行脚本标签的内部代码。 This is called micro template. 这称为微模板。 This concept is widely used in Single page application(aka SPA). 这个概念广泛用于单页面应用程序(又称SPA)。

For micro template, type of the script tag is text/template . 对于微模板,脚本标记的类型是text/template It is very well explained by Jquery creator John Resig Jquery创建者John Resig对此进行了很好的解释


#4楼

<script type = “text/template”> … </script> is obsolete. <script type = “text/template”> … </script>已过时。 Use <template> tag instead. 请改用<template>标签。


#5楼

Those script tags are a common way to implement templating functionality (like in PHP) but on the client side. 这些脚本标记是实现模板功能的常用方法(如在PHP中),但在客户端。

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. 通过将类型设置为“text / template”,它不是浏览器可以理解的脚本,因此浏览器将忽略它。 This allows you to put anything in there, which can then be extracted later and used by a templating library to generate HTML snippets. 这允许您在其中放置任何内容,然后可以在以后提取并由模板库使用以生成HTML片段。

Backbone doesn't force you to use any particular templating library - there are quite a few out there: , , , , and so on (the one used in the example you linked to is ). Backbone不会强迫您使用任何特定的模板库 - 那里有很多: , , , 等等(您链接到的示例中使用的是 )。 These will use their own syntax for you to write within those script tags. 这些将使用自己的语法在这些脚本标记内编写。


#6楼

To add to Box9's answer: 要添加到Box9的答案:

Backbone.js is dependent on underscore.js, which itself implements John Resig's original microtemplates. Backbone.js依赖于underscore.js,它本身实现了John Resig的原始微模板。

If you decide to use Backbone.js with Rails, be sure to check out the Jammit gem. 如果您决定将Backbone.js与Rails一起使用,请务必查看Jammit gem。 It provides a very clean way to manage asset packaging for templates. 它提供了一种非常干净的方式来管理模板的资产包装。

By default Jammit also uses JResig's microtemplates, but it also allows you to replace the templating engine. 默认情况下,Jammit也使用JResig的微模板,但它也允许您替换模板引擎。

转载地址:http://nzjnb.baihongyu.com/

你可能感兴趣的文章
JPA 联合主键配置
查看>>
ObjectAlreadyExistsException:Unable to store Job : '*', because one already exists with thi s ident
查看>>
mybatis & JPA 实体类型属性转换
查看>>
Git 中的 ~ 和 ^
查看>>
第一篇博客,给大家分享java、架构师、大数据、人工智能的学习路线,希望能够帮助到大家
查看>>
18级大数据专家,跟大家漫谈大数据平台架构,你能学到多少?上篇
查看>>
18级大数据专家,漫谈大数据平台安全风险与建设,值得学(下篇)
查看>>
阿里P8终于整理出:Nginx+jvm+MySQL+Docker+Spring实战技术文档
查看>>
腾讯T4专家精心整理:大数据+机器学习+数据挖掘+算法大集结
查看>>
阿里P8终于总结出:SpringBoot+Tomcat+Nginx+Netty面试题及答案
查看>>
阿里P7大牛,深入剖析JVM底层设计原理+高级特性pdf,附46页ppt
查看>>
史上最全141道大数据面试题:Redis+Linux+kafka+Hadoop,附答案
查看>>
一文带你深入理解JVM,看完之后你还敢说你懂JVM吗?颠覆you认知
查看>>
这些大厂面试真题你能答出来,年薪至少30-50W,想不想挑战一下?
查看>>
携程T7用637页PDF,解读十余热门技术领域,八场携程技术沙龙干货
查看>>
开发框架SpringBoot:构建SpringBoot工程+配置文件详解+Actuator
查看>>
6年拉力工作经验,学了阿里P8级架构师的7+1+1落地项目,跳槽阿里年薪直接40W+
查看>>
90天吃透阿里P8推荐的625页Java编程兵书技术实战,直接入职阿里定级P6
查看>>
清华毕业扫地僧,用157集终于把java给讲完了,总计3.13GB
查看>>
清华毕业大佬用了一个项目就把SpringMVC+Spring+MyBatis给讲完了
查看>>