欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS中的art-template模板如何使用if判斷

 更新時間:2022年09月05日 10:27:15   作者:Shawyu_  
這篇文章主要介紹了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> //使用大括號的方式輸出數(shù)據(jù)
?<p>{{1+1}}</p>//在括號內(nèi)可以實現(xiàn)基本運算
?<p>{{1+1==2?'相等':'不相等'}}</p>//在括號內(nèi)可以實現(xiàn)三目運算
?{{@ content }}//如果要引入包含html標(biāo)簽的數(shù)據(jù) 標(biāo)準(zhǔn)語法必須在中括號前加上@

2.原始語法

?<p><%=name%></p>
?<p><%=1+1==2?'相等':'不相等'%></p>
?<p><%- content%></p>//要引入包含html標(biāo)簽的數(shù)據(jù),就要把=號換成-

二、if判斷語句

1.標(biāo)準(zhǔn)語法

? ? ? {{if age>18}} 年齡大于18
? ? ? {{else if age<15}}年齡小于15
? ? ? {{else}}年齡不符合要求
? ? ? {{/if}}

2.原始語法

//其實就是先用<%%>把整個判斷語句包含起來 ?然后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語句的原始語法一樣 ?其實也是把整個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')%>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論