art-template模板渲染及其過濾器


原生語法

  使用原生語法,需要導入template-native.js文件。在HTML中定義模板,注意模板的位置,不要放到被渲染區域,防止模板丟失。

 1 <script id="tpl" type="text/html">
 2     <% for (var i = 0; i < products.length; i ++) { %>
 3         <% var product =products[i]; %>
 4         <% if (i < 3) { %>
 5             <li>
 6                 <img src="<%=getImageUrl(product.pictographicIconList[0].image.url)%>" data-imgname="<%=product.pictographicIconList[0].image.url%>">
 7                 <div class="flash-time-box">
 8                     <span>2015-02-09</span>
 9                 </div>
10                 <strong class="marque"><%=product.name%></strong>
11                 <strong class="libelle"><%=product.description%></strong>
12                 <div class="no-picto">
13                     <span class="prod-tip">
14                         <img src="img/grey.png" data-original="img/icon.png">
15                     </span>
16                     <span class="italic black">
17                         <span class="cny-curr">¥&nbsp;<%=formatPrice(product.promoPrice,'integer')%></span><span class="decimal"><%=formatPrice(product.promoPrice,'decimal')%></span>
18                     </span>
19                 </div>
20             </li>
21         <% } %>
22     <% } %>
23 </script>

template(id, data)

  渲染數據到頁面

$('#main_panel').html(template('tpl', data));

簡潔語法

  使用簡潔語法,導入template-web.js文件。

 1 <script id="tpl" type="text/html">
 2    {{each products as product i}}
 3    {{if i < 3}}
 4        <li>
 5            <img src="{{product.pictographicIconList[0].image.url | getImageUrl}}" data-imgname="{{product.pictographicIconList[0].image.url}}">
 6            <div class="flash-time-box">
 7                <span>2015-02-09</span>
 8            </div>
 9            <strong class="marque">{{product.name}}</strong>
10            <strong class="libelle">{{product.description}}</strong>
11            <div class="no-picto">
12                 <span class="prod-tip">
13                     <img src="img/grey.png" data-original="img/icon.png">
14                 </span>
15                 <span class="italic black">
16                     <span class="cny-curr">¥&nbsp;{{product.price.value | formatPrice: 'integer'}}</span><span class="decimal">{{product.price.value | formatPrice: 'decimal'}}</span>
17                 </span>
18            </div>
19        </li>
20    {{/if}}
21    {{/each}}
22 </script>

  渲染數據到頁面,和原生語法一樣

$('#main_panel').html(template('tpl', data));

  如果是下邊的這種渲染方式

$('#main_panel').html(template('tpl', data.products)); // 傳入的是數組

  那么在each循環中就應該為:{{each $data as product i }}

 

 

art-template 條件表達式

{{if admin}}
    <p>admin</p>
{{else if code > 0}}
    <p>master</p>
{{else}}
    <p>error!</p>
{{/if}}

 

 模板包含表達式

  用於嵌入子模板。

{{include 'template_name'}}

  子模板默認共享當前數據,亦可以指定數據:

{{include 'template_name' news_list}}

 

art-template過濾器

 語法:

template.defaults.imports.過濾器名稱 = function(date){
    過濾器的內容
    一定要注意 需要一個返回值
};

   

 

art-template嵌套循環

// 模板
<script type="text/html" id="phone_tpl">
{{each data v i}}
{{if v.type==2}}
<img src="{{fileUrl}}{{v.message}}" alt="">
{{else if v.type==1}}
{{include 'text_tpl' v.text}}
{{/if}}
{{/each}}
</script>

<script type="text/html" id="text_tpl">
{{each $data vv ii}}
<div class="phone-text">{{vv}}</div>
{{/each}}
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM