art-template辅助函数和子模板

art-template 前端使用

用途:主要用来处理数据和优化性能,与其他的一些模块化处理数据的插件相比,art-template处理性能好

不废话,上代码 

1.art-template基本语法使用

  <script src="template.js"></script>

  //id为模块的名称

  //语法全部为双标签  {{}}  里面为变量

  <script id="template" type="text/html">

  {{if films.length == 0}}
  <p>没有推荐的电影给您</p>
  {{else}}
  <h1>{{title}} : {{films.length}} </h1>
  <ul>
  {{each films as film index}}
  <li>
  {{film.name}}
  <del>{{film.normalPrice }}</del>
  <span>{{film.nowPrice }}</span>
  <br>
  首映日期:{{film.time}}
  </li>
  {{/each}}
  </ul>
  {{/if}}  

模拟数据:  

var data =
{
title : '推荐的电影' ,
films :
[
{
name : '湄公河公案' ,
normalPrice : 40 ,
nowPrice : 29.9 ,
time : '2016-6-6'
},
{
name : '重返二十岁' ,
normalPrice : 26 ,
nowPrice : 13 ,
time : '2016-12-12'
},
{
name : '长城' ,
normalPrice : 42 ,
nowPrice : 39.9 ,
time : '2017-12-25'
},
{
name : '倩女幽魂7' ,
normalPrice : 80 ,
nowPrice : 80 ,
time : '2018-8-8'
},
{
name : '程序员纪录片--单身汪的成长' ,
normalPrice : 1000 ,
nowPrice : 2000 ,
time : '2020-20-20'
}
]
}

  此处获取数据,并将数据交给template进行处理

  var html = template('template',data);
  渲染页面
  document.body.innerHTML = html

</script>

 

2.辅助函数,其实就是对一些数据做一些处理

price为辅助方法的名字    price_data为处理的数据

方法: template.helper('price',function(price_data){

  //处理的内容

})

以以上代码为例,为film.normalPrice和film.nowPrice数据添加一个¥修饰符

template.helper('price',function(price_data){

  return '¥' price_data.toFixed(2)

})

在处理的数据处用  '| 方法名'

<del>{{film.normalPrice | price}}</del>
<span>{{film.nowPrice | price}}</span>

 

3.模板引入子模板

 不多说,上demo

<script id="web" type="text/html">
  <a href="{{url}}">{{name}}</a>
  <br>
</script>

<script id="book" type="text/html">
  <img src="{{url}}" alt="">
  <br>
  <div>{{name}}</div>
</script>

<script id="recommend" type="text/html">

{{if items.length == 0}}
  <h1>抱歉,未找到推荐的相关内容</h1>
{{else}}
  <h1>{{title}}:{{items.length}}个</h1>

  {each items as item}}

  {{if item.type == 'web'}}
    {{include 'web' item}}
  {{else}}
    {{include 'book' item}}  //include用于引入子模块  'book'模块的id   item传递过去的数据
  {{/if}}
  {{/each}}
{{/if}}
</script>

再献上数据

var data =
{
title : '推荐的书籍和网站' ,
items:
[
{
type : 'web',
name : 'github' ,
url : 'https://github.com'
},
{
type : 'book' ,
name : '平凡的世界' ,
url : 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1484047575704&di=3bc7d59698a2aaeb917598e563aa749e&imgtype=0&src=http://imgsrc.baidu.com/forum/w%3D580/sign=08c992b4e9c4b7453494b71efffd1e78/ba14695c10385343f96e93bc9113b07ecb80884c.jpg'
},
{
type : 'web' ,
name : 'google' ,
url : 'https://google.com'
},
{
type : 'book' ,
name : '围城' ,
url : 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1484047692883&di=edf1860dc373863422ccdfecd43c1057&imgtype=0&src=http://ec4.images-amazon.com/images/I/415ZJgXDNEL._SL500_AA300_.jpg'
},
{
type : 'book' ,
name : '汤姆索亚历险记' ,
url : 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1484047764487&di=e6853f746fe6ba15b34266592b8501ac&imgtype=0&src=http://i1.w.hjfile.cn/doc/201111/tom%20sawyer11530.jpg'
},
]
}
var result = template('recommend',data) ;

document.body.innerHTML = result ;

 

再服务器使用art-template模块利用template引擎可实现项目模块化,具体。。。、

 

官方源码及文档:

https://github.com/aui/artTemplate

语法:

https://github.com/aui/artTemplate/wiki/syntax:simple

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值