标签:
普通使用
<script id="test" type="text/html"> {{if isAdmin}} <h1>{{title}}</h1> <ul> {{each list as value index}} <li>索引:{{index}}:{{value}}</li> {{/each}} </ul> {{/if}} </script> <script> var data = { title : ‘HELLO WORLD‘, isAdmin : true, list : [‘新闻‘,‘军事‘,‘历史‘,‘政治‘] };
var html = template(‘test‘, data); </script>
no-escape 不转义HTML
<script id="noEscape" type="text/html"> <p>不转义:{{#text}}</p> <p>默认转义: {{text}}</p> </script> <script> var data_noEscape = { text: ‘<span style="color:#F00">hello world!</span>‘ };
var html_noescape = template("noEscape", data_noEscape); </script>
在javascript中存放模板
<script> var source = ‘<ur>‘ + ‘{{each list}}‘+ ‘<li>索引:{{$index+1}}:{{$value}}</li>‘+ ‘{{/each}}‘+ ‘</ul>‘;
var data = { list : [‘电影‘,‘电视剧‘,‘综艺‘,‘音乐‘] };
var render = template.compile(source); var html = render(data); </script>
辅助方法
<script id="templateCont" type="text/html"> {{time | myFunc:‘...‘}} </script> <script> function myFunc(arg){ ... } var data = { time: 1408536771253, }; template.helper("myFunc", myFunc); var html = template(‘templateCont‘, data); </script>
标签:
原文地址:http://www.cnblogs.com/liuzhenwei/p/4791065.html