artTemplate模板引擎的使用

时间: 2019-03-14阅读: 181标签: 模板

artTemplate是腾讯开源的前端模板框架,和mustache,handlerbars类似,在web项目中可以很方便的使用,上手快,如果用过mustache,那么几乎可以快速切换到template框架上来。  


artTemplate特性

  • 拥有接近 JavaScript 渲染极限的的性能
  • 调试友好:语法、运行时错误日志精确到模板所在行;支持在模板文件上打断点(Webpack Loader)
  • 支持压缩输出页面中的 HTML、CSS、JS 代码
  • 支持 Express、Koa、Webpack
  • 支持模板继承与子模板
  • 兼容EJS、Underscore、LoDash 模板语法
  • 模板编译后的代码支持在严格模式下运行
  • 支持 JavaScript 语句与模板语法混合书写
  • 支持自定义模板的语法解析规则
  • 浏览器版本仅 6KB 大小

1.简单的json数据

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/template.js" ></script>
    </head>
    <body>
        <div id="container"></div>
        <script type="text/html" id="div">
            <h1>{{name}}</h1>
            <h2>{{age}}</h2>
        </script>
        <script type="text/javascript">
            var data = {
                name:"geekWeb",
                age:22
            }
            var html = template('div',data);
            document.getElementById("container").innerHTML = html;
        </script>
    </body>
</html>


2.带数组的json数据

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/template.js" ></script>
    </head>
    <body>
        <div id="container"></div>
        <script type="text/html" id="div">
            <h1>{{name}}</h1>
            <ul>
                {{each lang as value i}}
                <li>语言{{i+1}}:{{value}}</li>
                {{/each}}
            </ul>
        </script>
        <script type="text/javascript">
            var data = {
                name:"前端语言",
                lang:['html','css','js']
            }
            var html = template('div',data);
            document.getElementById("container").innerHTML = html;
        </script>
    </body>
</html>


3.数组中包含对象的json数据

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/template.js" ></script>
    </head>
    <body>
        <div id="container"></div>
        <script type="text/html" id="div">
            <h1>{{name}}</h1>
            <ul>
                {{each lang}}
                <li>语言:{{$value.title}} 定义:{{$value.add}}</li>
                {{/each}}
            </ul>
        </script>
        <script type="text/javascript">
            var data = {
                name:"前端语言",
                lang:[{
                    title:"html",
                    add:"超文本标记语言"
                },{
                    title:"css",
                    add:"层叠样式表"
                },{
                    title:"javaScript",
                    add:"添加动态特效"
                }]
            }
            var html = template('div',data);
            document.getElementById("container").innerHTML = html;
        </script>
    </body>
</html>


一个ajax+artTemplate+servlet+fastjson的综合小实例

前台:


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
    <script src="template.js"></script>
    <script>
      //创建一个XMLHttpRequest类型的对象ajaxReq
      var ajaxReq = new XMLHttpRequest();
      window.onload = function() {
        //用ajaxReq打开一个连接
        ajaxReq.open("get","ajax.do",true);
        //发送请求给服务器
        ajaxReq.send(null);
        //设置一个回调函数,用来处理服务器的回应。
        ajaxReq.onreadystatechange = onReceive;
      }

      function onReceive() {
        if (ajaxReq.readyState === 4) {
          if (ajaxReq.status === 200) {
            //响应成功,做一些事情
            var jsonObj = JSON.parse(ajaxReq.responseText);//不要忘记将json字符串转化为json对象
            var html = template('test', jsonObj);
            document.getElementById('content').innerHTML = html;
          } else {
            //响应失败,做一些事情
          }
        }
      };
    </script>
  </head>
  <body>
  <div id="content"></div>

  <script id="test" type="text/html">
    <table>
      <thead>
      <td>姓名</td>
      <td>年龄</td>
      </thead>
      {{each list as value i}}
      <tr>
        <td>{{value.name}}</td>
        <td>{{value.age}}</td>
      </tr>
      {{/each}}
    </table>
  </script>
  </body>
</html>

后台:

public class Person {
    public String name;
    public int    age;
}

public class Persons {
    private List<Person> list;

    public List<Person> getList() {
        return list;
    }

    public void setList(List<Person> list) {
        this.list = list;
    }
}

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ttc on 17-5-26.
 */
@WebServlet(name = "Servlet",urlPatterns = "/ajax.do")
public class Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Person person = new Person();
        person.name = "huochai";
        person.age = 29;

        String strJson = JSON.toJSONString(person);
        System.out.println(strJson);

        Person person2 = new Person();
        person2.name = "huochai2";
        person2.age = 292;

        List<Person> lst = new ArrayList<Person>();
        lst.add(person);
        lst.add(person2);

        Persons persons = new Persons();
        persons.setList(lst);

        String strJson2 = JSON.toJSONString(persons);
        System.out.println(strJson2);

        PrintWriter pw = response.getWriter();
        pw.println(strJson2);
    }
}

fly63.com版权所有,内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权,请与小编联系!情况属实本人将予以删除!

广告赞助文章投稿关于web前端网站点搜索站长推荐网站地图站长QQ:522607023

小程序专栏: 土味情话心理测试脑筋急转弯幽默笑话段子句子语录成语大全