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的基本使用(判斷語(yǔ)句、循環(huán)、子模板的使用)
//數(shù)據(jù)來(lái)源
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)語(yǔ)法
?<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)語(yǔ)法必須在中括號(hào)前加上@2.原始語(yǔ)法
?<p><%=name%></p> ?<p><%=1+1==2?'相等':'不相等'%></p> ?<p><%- content%></p>//要引入包含html標(biāo)簽的數(shù)據(jù),就要把=號(hào)換成-
二、if判斷語(yǔ)句
1.標(biāo)準(zhǔn)語(yǔ)法
? ? ? {{if age>18}} 年齡大于18
? ? ? {{else if age<15}}年齡小于15
? ? ? {{else}}年齡不符合要求
? ? ? {{/if}}2.原始語(yǔ)法
//其實(shí)就是先用<%%>把整個(gè)判斷語(yǔ)句包含起來(lái) ?然后if(){%><%}else if(){%><%}else{%><%}
? ? ? <% if(age>18){%>
? ? ? 年齡大于18
? ? ? <%}
? ? ? else if(age<15){%>年齡小于15<%}
? ? ? else{%>年齡不符合要求<%}
? ? ? %>三、for循環(huán)語(yǔ)句
//數(shù)據(jù)來(lái)源
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)語(yǔ)法
? ?<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.原始語(yǔ)法
<ul>
//跟if語(yǔ)句的原始語(yǔ)法一樣 ?其實(shí)也是把整個(gè)for循環(huán)語(yǔ)句用<%%>包含起來(lái) ? 然后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)語(yǔ)法
{{include './common/header.art'}}
<div>{{msg}}</div>
{{include './common/footer.art'}}2.原始語(yǔ)法
<% include ('./common/header.art')%>
<div><%=msg%></div>
<% include ('./common/footer.art')%>以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于javascript實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果
這篇文章主要為大家詳細(xì)介紹了基于javascript實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘效果的相關(guān)資料,動(dòng)態(tài)顯示系統(tǒng)當(dāng)前時(shí)間,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02
Javascript 數(shù)組添加 shuffle 方法的實(shí)現(xiàn)代碼
PHP 里面有個(gè)非常方便的打亂數(shù)組的函數(shù) shuffle() ,這個(gè)功能在許多情況下都會(huì)用到,但 javascript 的數(shù)組卻沒(méi)有這個(gè)方法,沒(méi)有不要緊,可以擴(kuò)展一個(gè),自己動(dòng)手,豐衣足食嘛。2009-09-09
JavaScript逐點(diǎn)突破系列之this是什么
本章將專門介紹與執(zhí)行上下文創(chuàng)建階段直接相關(guān)的最后一個(gè)細(xì)節(jié)——this是什么?以及它的指向到底是什么,感興趣的朋友跟隨小編一起看看吧2021-04-04
解決layui的table插件無(wú)法多層級(jí)獲取json數(shù)據(jù)的問(wèn)題
今天小編就為大家分享一篇解決layui的table插件無(wú)法多層級(jí)獲取json數(shù)據(jù)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
用Javascript實(shí)現(xiàn)錨點(diǎn)(Anchor)間平滑跳轉(zhuǎn)
本文介紹的方法,實(shí)現(xiàn)了錨點(diǎn)(Anchor)間平滑跳轉(zhuǎn),效果非常不錯(cuò)。2009-09-09
attachEvent的使用方法與傳遞參數(shù)[IE|firefox]

