", "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><script type = "text/template"></script></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><script id="hello" type="text/template"> Hello world </script> <script> alert($('#hello').html()); </script> </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" } } ] } }