", "text": "<p>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 <code>&lt;script type = "text/template"&gt;&lt;/script&gt;</code>, which contained code that looks like something out of PHP but with JavaScript tags.</p> <p>Can someone explain this to me? Is this legit?</p>", "answerCount": 2, "upvoteCount": 945, "dateCreated": "2011-02-06 09:46:07", "dateModified": "2022-09-18 18:33:01", "author": { "type": "Person", "name": "Matt" }, "acceptedAnswer": { "@type": "Answer", "text": "<p>Those script tags are a common way to implement templating functionality (like in PHP) but on the client side.</p> <p>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.</p> <p>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.</p>", "upvoteCount": 144, "url": "https://exchangetuts.com/explanation-of-script-type-texttemplate-script-1639462383515267#answer-1646618594010212", "dateCreated": "2022-09-12 18:33:01", "dateModified": "2022-09-18 18:33:01", "author": { "type": "Person", "name": "David Tang" } }, "suggestedAnswer": [ { "@type": "Answer", "text": "<p>It's legit and very handy! </p> <p>Try this:</p> <pre class="prettyprint"><code>&lt;script id="hello" type="text/template"&gt; Hello world &lt;/script&gt; &lt;script&gt; alert($('#hello').html()); &lt;/script&gt; </code></pre> <p>Several Javascript templating libraries use this technique. Handlebars.js is a good example.</p>", "upvoteCount": 22, "url": "https://exchangetuts.com/explanation-of-script-type-texttemplate-script-1639462383515267#answer-1646618594012408", "dateCreated": "2022-09-14 18:33:01", "dateModified": "2022-09-16 18:33:01", "author": { "type": "Person", "name": "Rimian" } } ] } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of <script type = "text/template"> ... </script>

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?

like image 945
Matt Avatar asked Feb 06 '11 09:02

Matt


People also ask

What is script type in HTML?

<script>: The Script element. The <script> HTML element is used to embed executable code or data; this is typically used to embed or refer to JavaScript code.

What is the purpose of the script and </ script tags?

Definition and UsageThe <script> tag is used to embed a client-side script (JavaScript). The <script> element either contains scripting statements, or it points to an external script file through the src attribute. Common uses for JavaScript are image manipulation, form validation, and dynamic changes of content.

What is the meaning of script type text JavaScript?

The HTML script tag <script> is used to embed data or executable client side scripting language in an HTML page. Mostly, JavaScript or JavaScript based API code inside a <script></script> tag.

What is the difference if any between script </ script and script type text/JavaScript </ script >?

text/javascript is the default type, so you can omit it, it makes no difference. Nothing, except in very special cases with old IE where if the first script element set the type to some other scripting language (e.g. VBScript) then that was the default if no type was specified.


2 Answers

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

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,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.

like image 144
David Tang Avatar answered Sep 18 '22 18:09

David Tang


It's legit and very handy!

Try this:

<script id="hello" type="text/template">   Hello world </script> <script>   alert($('#hello').html()); </script> 

Several Javascript templating libraries use this technique. Handlebars.js is a good example.

like image 22
Rimian Avatar answered Sep 16 '22 18:09

Rimian