Background
All the JS template engines recommend putting your template text inside script blocks like so:
<script id="peopleTemplate" type="text/template">
{#people}
<div class="person">Name: {firstName} {lastName}</div>
{/people}
</script>
but many developers (understandably) dislike this because they lose HTML syntax highlighting in their code editor inside the script block. I've seen the workarounds like this: Keep correct HTML syntax highlighting in <script> "text/html" templates . This question is not asking about workarounds.
I know one danger is that web browsers will attempt to fix invalid HTML so calling $('#peopleTemplate').html() will have unpredictable results. However, off the top of my head, I cannot think of any examples to illustrate this.
Question
What are some of the other dangers of replacing the script tag with a div ?
Bonus: can someone come up with a good fiddle that illustrates the browser HTML auto-fixing?
<li>
is invalid HTML, the browser is free to wrap it in a<ul>
to correct it. A template can contain multipleid="{{id}}"
attributes which will be valid when filled in but are invalid HTML until then. Not lying to the browser is an easy way to future-proof your code; just because something invalid doesn't break today doesn't mean it won't break in the next point release. – Aquanaut<script type="text/javascript" src="template.js"></script>
. – Selfinterest