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%3A%2F%2Fimgsrc.baidu.com%2Fforum%2Fw%253D580%2Fsign%3D08c992b4e9c4b7453494b71efffd1e78%2Fba14695c10385343f96e93bc9113b07ecb80884c.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%3A%2F%2Fec4.images-amazon.com%2Fimages%2FI%2F415ZJgXDNEL._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%3A%2F%2Fi1.w.hjfile.cn%2Fdoc%2F201111%2Ftom%2520sawyer11530.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

art-template辅助函数和子模板的更多相关文章

  1. art template前端模板引擎

    偶然看到后台有一段代码 采用的是art template的模板引擎 地址为 http://aui.github.io/artTemplate/ 这段代码很简洁 var html = template( ...

  2. Vue最常用的组件通讯有三种:父->子组件通讯、子->父组件通讯,兄弟组件通讯.(template用的pug模板语法)

    Vue组件通讯   Vue最常用的组件通讯有三种:父->子组件通讯.子->父组件通讯,兄弟组件通讯.(template用的pug模板语法) 1.父->子组件通讯 父->子组件通 ...

  3. js 模板引擎 -Art Template

    一个例子涵盖所有: <!doctype html> <html> <head> <meta charset="UTF-8"> < ...

  4. JavaScript模板引擎artTemplate.js——引入子模板

    之前的例子都是单一结构的对象,如果遇到复杂对象结构,我们可以通过引入子模板来实现html的渲染. 依旧以之前的数据作为例子: <div id="content">< ...

  5. 在引用的laravel的@include子模板中传递参数

    调用传参: @include("message",['msg'=>'中国']) 在message子模板中调用msg的值: {{msg}}

  6. artTemplate子模板include

    art.Template:https://github.com/aui/art-template 下面来实现利用模版来实现递归调用生成tree <script type="text/h ...

  7. ZendFramework-2.4 源代码 - 关于MVC - View层 - 在模板内渲染子模板

    <?php // 方式一: // 1.在模板内直接编写如下内容即可 $viewModel = new ViewModel(); $viewModel->setTemplate('album ...

  8. Template 基础篇-函数模板(待看

    Template 基础篇-函数模板 Template所代表的泛型编程是C++语言中的重要的组成部分,我将通过几篇blog对这半年以来的学习做一个系统的总结,本文是基础篇的第一部分. Template ...

  9. WordPress子模板继承

    很多时候我们不想重写模板,而是想在某个模板的基础上进行修改,那么这个时候我们就需要用到模板继承技巧. 子主题开发 style.css 是必须的文件,只需要新增 Template: 父模板的文件夹名

随机推荐

  1. vue路由router的三种传参方式

    方法三: 传参页面传递参数方式: this.$router.push({ path: 'indexTwoDetails', query: { "id": id } }) 接受参数页 ...

  2. MATLAB下数组随机打乱顺序的方法

    一:问题 有两个规模相同的数组,两个数组相同位置的元素一一对应,现在要将两数组的元素同时打乱顺序,并且乱序后的两数组对应位置元素要保持乱序前的对应关系. 二:方法  采用randperm()函数,产生 ...

  3. Hive中的Row_Number()使用

    语法:row_number() over (partition by 字段a order by 计算项b desc ) rank --这里rank是别名 partition by:类似hive的建表, ...

  4. 设置JVM参数的几种方式解决java.lang.OutOfMemoryError:Java heap space

    一.首先给出查询当前JVM内存的代码: 下面是查询当前JVM 内存大小的代码,可以测试设置后JVM 的内存是否会变化.增加JVM 内存的配置项后,无需重新启动eclipse .具体的代码如下: pub ...

  5. 线程中的同步辅助类Exchanger

    Exchanger 允许两个线程在 collection 点交换对象,它在多流水线设计中是有用的. 允许两条线程之间交换数据.Exchanger的exchange方法是阻塞的,当其他线程也调用了该方法 ...

  6. VS2013编译的exe独立运行在XP中方案

    转载知乎 现在,我们深入探讨一下:<如何使用VS 2013发布一个可以在Windows XP中独立运行的可执行文件>. 这个问题是比较常见且容易造成初学者困惑的,作为曾经撞了无数次南墙的初 ...

  7. Vue + Element UI 实现权限管理系统 前端篇(七):功能组件封装

    组件封装 为了避免组件代码的臃肿,这里对主要的功能部件进行封装,保证代码的模块化和简洁度. 组件结构 组件封装重构后,试图组件结构如下图所示 代码一览 Home组件被简化,包含导航.头部和主内容三个组 ...

  8. Spring框架引入

    Struts与Hibernate可以做什么事? Struts, Mvc中控制层解决方案 可以进行请求数据自动封装.类型转换.文件上传.效验… Hibernate, 持久层的解决方案: 可以做到, 把对 ...

  9. Normal Map中的值, Tangent Space, 求算 Tangent 与 Binormal 与 TBN Matrix

      - Normal Map中的值 -   有没有想过,Normal Map(法线贴图)为什么看上去都是“偏蓝色”的?这是因为,在map中存储的值都是在Tangent Space(切空间)下的.比如, ...

  10. Java类MemoryUsage查看虚拟机的使用情况

    原文地址:https://www.cnblogs.com/xubiao/p/5465473.html Java类MemoryUsage,通过MemoryUsage可以查看Java 虚拟机的内存池的内存 ...