JS中的art-template模板如何使用if判斷
JS art-template模板使用if判斷
JS代碼:
?? ?// json數(shù)據(jù)
?? ?var json=[
?? ??? ?{
?? ??? ??? ?"id": 1,
?? ??? ??? ?"good_sign": 2,
?? ??? ??? ?"good_img": "http://dummyimage.com/460x440/ee79f2/79f2cb.png&text=商品\n"
?? ??? ?},
?? ??? ?{
?? ??? ??? ?"id": 2,
?? ??? ??? ?"good_sign": 1,
?? ??? ??? ?"good_img": "http://dummyimage.com/460x440/f2a779/8479f2.png&text=商品\n"
?? ??? ?},
?? ??? ?{
?? ??? ??? ?"id": 3,
?? ??? ??? ?"good_sign": 0,
?? ??? ??? ?"good_img": "http://dummyimage.com/460x440/91f279/f279b4.png&text=商品\n"
?? ??? ?},
?? ??? ?{
?? ??? ??? ?"id": 4,
?? ??? ??? ?"good_sign": 1,
?? ??? ??? ?"good_img": "http://dummyimage.com/460x440/79d7f2/f2e979.png&text=商品\n"
?? ??? ?}
?? ?]
?? ?// 渲染json
?? ?$("#container").html(template("indexmain",json));HTML代碼:
<div id="container"></div>
<script type="text/html" id="indexmain">
<!-- 商品列表 -->
<ul>
{{each list item}}
<li>
<a href="javascript:;" rel="external nofollow" >
{{if item.good_sign==1}}
<div class="ico-comm i-mark">
<span>新品</span>
</div>
{{else if item.good_sign==2}}
<div class="ico-comm i-mark">
<span>熱賣</span>
</div>
{{/if}}
<img src="{{item.good_img}}" alt="商品圖">
</a>
</li>
{{/each}}
</ul>
</script>
效果圖:

模板引擎art-template的基本使用
art-template的基本使用(判斷語句、循環(huán)、子模板的使用)
//數(shù)據(jù)來源
const template = require('art-template');
const path = require('path');
const views = path.join(__dirname, 'views', '02.art');
const html = template(views, {
? ? name: '張三',
? ? age: 17,
? ? content: '<h1>我是標(biāo)題</h1>'
})
console.log(html);一、輸出數(shù)據(jù)
1.標(biāo)準(zhǔn)語法
?<p>{{ name }}</p> //使用大括號(hào)的方式輸出數(shù)據(jù)
?<p>{{1+1}}</p>//在括號(hào)內(nèi)可以實(shí)現(xiàn)基本運(yùn)算
?<p>{{1+1==2?'相等':'不相等'}}</p>//在括號(hào)內(nèi)可以實(shí)現(xiàn)三目運(yùn)算
?{{@ content }}//如果要引入包含html標(biāo)簽的數(shù)據(jù) 標(biāo)準(zhǔn)語法必須在中括號(hào)前加上@2.原始語法
?<p><%=name%></p> ?<p><%=1+1==2?'相等':'不相等'%></p> ?<p><%- content%></p>//要引入包含html標(biāo)簽的數(shù)據(jù),就要把=號(hào)換成-
二、if判斷語句
1.標(biāo)準(zhǔn)語法
? ? ? {{if age>18}} 年齡大于18
? ? ? {{else if age<15}}年齡小于15
? ? ? {{else}}年齡不符合要求
? ? ? {{/if}}2.原始語法
//其實(shí)就是先用<%%>把整個(gè)判斷語句包含起來 ?然后if(){%><%}else if(){%><%}else{%><%}
? ? ? <% if(age>18){%>
? ? ? 年齡大于18
? ? ? <%}
? ? ? else if(age<15){%>年齡小于15<%}
? ? ? else{%>年齡不符合要求<%}
? ? ? %>三、for循環(huán)語句
//數(shù)據(jù)來源
const template = require('art-template');
const path = require('path');
const views = path.join(__dirname, 'views', '03.art');
const html = template(views, {
? ? users: [{
? ? ? ? name: '張三',
? ? ? ? age: 20,
? ? ? ? sex: '男'
? ? }, {
? ? ? ? name: '李四',
? ? ? ? age: 30,
? ? ? ? sex: '男'
? ? }, {
? ? ? ? name: '瑪麗',
? ? ? ? age: 15,
? ? ? ? sex: '女'
? ? }]
});
console.log(html);1.標(biāo)準(zhǔn)語法
? ?<ul>
? ? ?{{each users}}//users 就是被循環(huán)的數(shù)據(jù)
? ? ?<li>{{$value.name}}</li>//value就是循環(huán)得出的數(shù)據(jù)
? ? ?<li>{{$value.age}}</li>
? ? ?<li>{{$value.sex}}</li>
? ? ?{{/each}}
? ? ?</ul>2.原始語法
<ul>
//跟if語句的原始語法一樣 ?其實(shí)也是把整個(gè)for循環(huán)語句用<%%>包含起來 ? 然后for(){%><%} ?里面js的for怎么寫 ?這里還是怎么寫
? ? ? ? <% for(var i=0;i<users.length;i++){%>
? ? ? ? <li><%=users[i].name%></li>
? ? ? ? <li><%=users[i].age%></li>
? ? ? ? <li><%=users[i].sex%></li>
? ? ? ? <%} %>
? ? ?</ul>四、子模板
1.標(biāo)準(zhǔn)語法
{{include './common/header.art'}}
<div>{{msg}}</div>
{{include './common/footer.art'}}2.原始語法
<% include ('./common/header.art')%>
<div><%=msg%></div>
<% include ('./common/footer.art')%>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
ie7+背景透明文字不透明超級(jí)簡(jiǎn)單的實(shí)現(xiàn)方法
這篇文章主要介紹了ie7+背景透明文字不透明超級(jí)簡(jiǎn)單的實(shí)現(xiàn)方法,有需要的朋友可以參考一下2014-01-01
JS獲取鼠標(biāo)坐標(biāo)并且根據(jù)鼠標(biāo)位置不同彈出不同內(nèi)容
這篇文章主要介紹了js獲取鼠標(biāo)坐標(biāo)并且根據(jù)鼠標(biāo)位置不同彈出不同內(nèi)容的實(shí)例代碼,需要的朋友可以參考下2017-06-06
JavaScript性能陷阱小結(jié)(附實(shí)例說明)
JavaScript陷阱太多,因此我們得步步為營(yíng),下面這些點(diǎn),相信很多同學(xué)都會(huì)遇到,希望朋友們多注意下。JavaScript陷阱太多,因此我們得步步為營(yíng),下面是一些常見的影響性能的陷阱。2010-12-12
js對(duì)table的td進(jìn)行相同內(nèi)容合并示例詳解
正如標(biāo)題所言如何對(duì)table的td進(jìn)行相同內(nèi)容合并,下面為大家詳細(xì)介紹下使用js是如何做到的,感興趣的朋友不要錯(cuò)過2013-12-12
利用ES6的Promise.all實(shí)現(xiàn)至少請(qǐng)求多長(zhǎng)時(shí)間的實(shí)例
下面小編就為大家?guī)硪黄肊S6的Promise.all實(shí)現(xiàn)至少請(qǐng)求多長(zhǎng)時(shí)間的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
javascript字符串替換函數(shù)如何一次性全部替換掉
這篇文章主要介紹了JS字符串替換函數(shù)replace如何一次性全部替換的相關(guān)資料,需要的朋友可以參考下2015-10-10

