artTemplate的使用总结

作者: ilaoke | 来源:发表于2015-07-22 23:43 被阅读79730次

artTemplate

原生语法

使用原生语法,需要导入template-native.js文件。

在HTML中定义模板,注意模板的位置,不要放到被渲染区域,防止模板丢失。

<script id="main_panel_big_sale_template" type="text/html">
    <% for (var i = 0; i < products.length; i ++) { %>
        <% var product =products[i]; %>
        <% if (i < 3) { %>
            <li>
                <img src="<%=getImageUrl(product.pictographicIconList[0].image.url)%>" data-imgname="<%=product.pictographicIconList[0].image.url%>">
                <div class="flash-time-box">
                    <span>2015-02-09</span>
                </div>
                <strong class="marque"><%=product.name%></strong>
                <strong class="libelle"><%=product.description%></strong>
                <div class="no-picto">
                    <span class="prod-tip">
                        <img src="img/grey.png" data-original="img/icon.png">
                    </span>
                    <span class="italic black">
                        <span class="cny-curr">¥&nbsp;<%=formatPrice(product.promoPrice,'integer')%></span><span class="decimal"><%=formatPrice(product.promoPrice,'decimal')%></span>
                    </span>
                </div>
            </li>
        <% } %>
    <% } %>
</script>

template(id, data)

渲染数据到页面

$('#main_panel').html(template('main_panel_big_sale_template', data));

简洁语法

使用简洁语法,导入template.js文件。

<script id="main_panel_big_sale_template" type="text/html">
   {{each products as product i}}
   {{if i < 3}}
       <li>
           <img src="{{product.pictographicIconList[0].image.url | getImageUrl}}" data-imgname="{{product.pictographicIconList[0].image.url}}">
           <div class="flash-time-box">
               <span>2015-02-09</span>
           </div>
           <strong class="marque">{{product.name}}</strong>
           <strong class="libelle">{{product.description}}</strong>
           <div class="no-picto">
                <span class="prod-tip">
                    <img src="img/grey.png" data-original="img/icon.png">
                </span>
                <span class="italic black">
                    <span class="cny-curr">¥&nbsp;{{product.price.value | formatPrice: 'integer'}}</span><span class="decimal">{{product.price.value | formatPrice: 'decimal'}}</span>
                </span>
           </div>
       </li>
   {{/if}}
   {{/each}}
</script>

渲染数据到页面,和原生语法一样

$('#main_panel').html(template('main_panel_big_sale_template', data));

调用外部函数

template.helper

上面的例子中,都调用了formatPrice函数,要调用此函数需要通过helper方法注册:

template.helper('formatPrice', function(price, type) {
    if(price){
        var arrayPrice = price.toString().split(".");
        if(type == 'integer') {
            return arrayPrice[0]?arrayPrice[0]:"0";
        }else if (type =='decimal') {
            return arrayPrice[1]?arrayPrice[1].length == 1?"."+arrayPrice[1]+"0":"."+arrayPrice[1]:".00";
        }
    }else{
        if(type == 'integer') {
            return "0";
        }else if (type =='decimal') {
            return ".00";
        }
    }
});

原生语法与简洁语法比较

语法类型 调用外部函数
原生 <%=formatPrice(product.promoPrice,'integer')%>
简洁 <code>{{product.price.value | formatPrice: 'integer'}}</code>

简洁语法的传参有点奇怪,原生语法就很好理解,如果要传递三个参数,简洁语法该怎么写呢?

简洁语法的循环如果要做更多逻辑,也实现不了

推荐使用原生语法

template.compile

模板可以直接写在JS中,再编译渲染。

var source = '<ul>'
+    '<% for (var i = 0; i < list.length; i ++) { %>'
+        '<li>索引 <%= i + 1 %> :<%= list[i] %></li>'
+    '<% } %>'
+ '</ul>';

var render = template.compile(source);
var html = render({list: ['摄影', '电影', '民谣', '旅行', '吉他']});
document.getElementById('content').innerHTML = html;

这种方式的的缺点是,模板通过字符串拼接,不好维护,适合简单模板。

相关文章

  • artTemplate的使用总结

    artTemplate 原生语法 使用原生语法,需要导入template-native.js文件。 在HTML中定...

  • artTemplate 总结

    编写模板 渲染模板 简介语法 方法 template(id,data) 根据id渲染模板,内部会根据documen...

  • artTemplate的使用

    用artTemplate已经有一段时间了,也是在比较了几款前端模版引擎之后决定使用它,因为它的使用方便以及性能卓越...

  • 前端模版引擎 - art-template 【下】

    我们接着上篇 《 前端模版引擎 - artTemplate 【上】 》 继续讲解 artTemplate 的最新版...

  • 百度模板

    引用script 模板语法 拓展: artTemplate artTemplate有两种语法,所以就有两...

  • JavaScript模板引擎-artTemplate-3.0

    性能卓越的js模板引擎:https://github.com/aui/artTemplate 一、为什么使用Jav...

  • artTemplate模板引擎的使用

    w

  • javaweb分页

    前端使用分页组件mricode+artTemplate 数据库 后端代码实体类User.java PageData...

  • 2019-03-18

    项目简介 前端 1.使用arttemplate模板引擎渲染页面模板。 2.通过gulp自动化构建前端内容。 3.使...

  • artTemplate前端模板引擎使用整理

    1.前言 ArtTemlate是一个前端渲染模板引擎,能异步操作下实现类似于jsp中C标签的数据渲染效果,具有稳定...

网友评论

  • 酒暖花深Q:为什么我把内容template到html里面的时候html的内容就变成了" "
    ilaoke:此文记录的文法可能已经不适用最新的artTemplate版本,建议参考最近的官方文档
  • mianwo:为什么不用vue
    f4f7f9289922:20KB的网站引一个80KB的框架进去?
    54cd6f563bdb:为什么要用vue?像只是单独的几个界面,一些独立的小模块小功能,vue、react、angular都不想用
    cc17ed98653a:我也想问 每个子页面都需要单独的引入jq等公共文件 这样不是很慢吗
  • 蜗牛Coming:你好,请教一下,我刚用arttemplate这个模板引擎,一直报template.helper()is not function,现在是最新的版本不支持template.helper()了吗?
    蜗牛Coming:@ilaoke 谢谢
    ilaoke:建议看下官方的文档,参考文档中 过滤器 https://aui.github.io/art-template/docs/syntax.html#过滤器
  • 梁同学de自言自语:artTemplate的作者是更推荐简洁语法(可以去它的GitHub看),你却更喜欢原生语法,醉了
    炼心92:https://github.com/aui/artTemplate/issues/154

    “简洁语法的目的是:让模板更清晰,同时也能使用到基本的逻辑。

    如果你需要更强大的语法,可以采用原生语法。”

    有些复杂数据处理情况确实还是原生更方便
    小羊装狼:artTemplate的作者推荐简洁语法是因为简洁语法更直观。本文作者推荐原生语法是因为原生语法功能更强大。感觉一个是前端美工向的,一个是程序员向的....
    ilaoke:本文只是个人使用后的总结,一切以官方文档为准..
  • 鬼颜兮:nice

本文标题:artTemplate的使用总结

本文链接:https://www.haomeiwen.com/subject/oonpqttx.html