handlebars.js - Why should we wrap our templates inside script blocks? -
background
all js template engines recommend putting template text inside script blocks so:
<script id="peopletemplate" type="text/template"> {#people} <div class="person">name: {firstname} {lastname}</div> {/people} </script>
but many developers (understandably) dislike because lose html syntax highlighting in code editor inside script block. i've seen workarounds this: keep correct html syntax highlighting in <script> "text/html" templates . question not asking workarounds.
i know 1 danger web browsers attempt fix invalid html calling $('#peopletemplate').html() have unpredictable results. however, off top of head, cannot think of examples illustrate this.
question
what of other dangers of replacing script tag div ?
bonus: can come fiddle illustrates browser html auto-fixing?
here's example of browser auto-fixing issue: http://jsfiddle.net/kpasf/
the template is:
<table> <tr> {{#numbers}} <th> {{.}} </th> {{/numbers}} </tr> </table>
the data is:
var json = { "numbers": [ 1, 2, 3 ] };
see fiddle http://jsfiddle.net/kpasf/ different results when template in hidden div, , again in script block.
explanation
when put in hidden <div>
, browser strip content outside <th>
tags (the {{#numbers}}
, {{#numbers}}
mustache tags). leaves {{.}}
, bind json object , render [object object]
putting template in <script type='text/html'>
block works expect, 3 <th>
's
example of how browser mangles template if housed inside <div>
instead of <script>
:
Comments
Post a Comment