随笔- 28 文章- 0 评论- 0 html
使用原生语法,须要导入template-native.js
文件。git
在HTML中定义模板,注意模板的位置,不要放到被渲染区域,防止模板丢失。github
<script id="main_panel_big_sale_template" type="text/html"> <% for (var i = 0; i < products.length; i ++) { %> <% var product =products[i]; %> <% if (i < 3) { %> <li> <img src="<%=getImageUrl(product.pictographicIconList[0].image.url)%>" data-imgname="<%=product.pictographicIconList[0].image.url%>"> <div class="flash-time-box"> <span>2015-02-09</span> </div> <strong class="marque"><%=product.name%></strong> <strong class="libelle"><%=product.description%></strong> <div class="no-picto"> <span class="prod-tip"> <img src="img/grey.png" data-original="img/icon.png"> </span> <span class="italic black"> <span class="cny-curr">¥ <%=formatPrice(product.promoPrice,'integer')%></span><span class="decimal"><%=formatPrice(product.promoPrice,'decimal')%></span> </span> </div> </li> <% } %> <% } %> </script>
渲染数据到页面函数
$('#main_panel').html(template('main_panel_big_sale_template', data));
使用简洁语法,导入template.js
文件。post
<script id="main_panel_big_sale_template" type="text/html"> {{each products as product i}} {{if i < 3}} <li> <img src="{{product.pictographicIconList[0].image.url | getImageUrl}}" data-imgname="{{product.pictographicIconList[0].image.url}}"> <div class="flash-time-box"> <span>2015-02-09</span> </div> <strong class="marque">{{product.name}}</strong> <strong class="libelle">{{product.description}}</strong> <div class="no-picto"> <span class="prod-tip"> <img src="img/grey.png" data-original="img/icon.png"> </span> <span class="italic black"> <span class="cny-curr">¥ {{product.price.value | formatPrice: 'integer'}}</span><span class="decimal">{{product.price.value | formatPrice: 'decimal'}}</span> </span> </div> </li> {{/if}} {{/each}} </script>
渲染数据到页面,和原生语法同样ui
$('#main_panel').html(template('main_panel_big_sale_template', data));
上面的例子中,都调用了formatPrice
函数,要调用此函数须要经过helper方法注册:url
template.helper('formatPrice', function(price, type) { if(price){ var arrayPrice = price.toString().split("."); if(type == 'integer') { return arrayPrice[0]?arrayPrice[0]:"0"; }else if (type =='decimal') { return arrayPrice[1]?arrayPrice[1].length == 1?"."+arrayPrice[1]+"0":"."+arrayPrice[1]:".00"; } }else{ if(type == 'integer') { return "0"; }else if (type =='decimal') { return ".00"; } } });
语法类型 | 调用外部函数 |
---|---|
原生 | <%=formatPrice(product.promoPrice,'integer')%> |
简洁 | {{product.price.value | formatPrice: 'integer'}} |
简洁语法的传参有点奇怪,原生语法就很好理解,若是要传递三个参数,简洁语法该怎么写呢?spa
简洁语法的循环若是要作更多逻辑,也实现不了。code
推荐使用原生语法orm
模板能够直接写在JS中,再编译渲染。
var source = '<ul>' + '<% for (var i = 0; i < list.length; i ++) { %>' + '<li>索引 <%= i + 1 %> :<%= list[i] %></li>' + '<% } %>' + '</ul>'; var render = template.compile(source); var html = render({list: ['摄影', '电影', '民谣', '旅行', '吉他']}); document.getElementById('content').innerHTML = html;
这种方式的的缺点是,模板经过字符串拼接,很差维护,适合简单模板
文/ilaoke(简书做者)
原文连接:http://www.jianshu.com/p/483fa7f6f55b
著做权归做者全部,转载请联系做者得到受权,并标注“简书做者”。
posted @ 2016-10-19 14:11 大猫一只 阅读(...) 评论(...) 编辑 收藏
Copyright ©2017 大猫一只