Can someone explain this to me? Is this legit?
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.
It’s legit and very handy!
<script id="hello" type="text/template"> Hello world </script> <script> alert($('#hello').html()); </script>
By setting script tag
type other than
<script type="text/template">I am a Micro template. I am going to make your web page faster.</script>
For micro template, type of the script tag is
To add to Box9’s answer:
Backbone.js is dependent on underscore.js, which itself implements John Resig’s original microtemplates.
If you decide to use Backbone.js with Rails, be sure to check out the 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.
It’s a way of adding text to HTML without it being rendered or normalized.
It’s no different than adding it like:
<script type = “text/template”> … </script> is obsolete. Use
<template> tag instead.