发现一枚前端数据绑定导弹:laytpl,官网:http://www.layui.com/laytpl/
为了不用angularJS等较为重量级的,和繁琐的配置,所以就用了laytpl,可以配合JQ使用
laytpl.js源码如下:
/** @Name:laytpl-v1.1 精妙的js模板引擎 @Author:贤心 - 2014-08-16 @Site:http://sentsin.com/layui/laytpl @License:MIT license */ ;!function(){"use strict";var f,b={open:"{{",close:"}}"},c={exp:function(a){return new RegExp(a,"g")},query:function(a,c,e){var f=["#([\\s\\S])+?","([^{#}])*?"][a||0];return d((c||"")+b.open+f+b.close+(e||""))},escape:function(a){return String(a||"").replace(/&(?!#?[a-zA-Z0-9]+;)/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/‘/g,"'").replace(/"/g,""")},error:function(a,b){var c="Laytpl Error:";return"object"==typeof console&&console.error(c+a+"\n"+(b||"")),c+a}},d=c.exp,e=function(a){this.tpl=a};e.pt=e.prototype,e.pt.parse=function(a,e){var f=this,g=a,h=d("^"+b.open+"#",""),i=d(b.close+"$","");a=a.replace(/[\r\t\n]/g," ").replace(d(b.open+"#"),b.open+"# ").replace(d(b.close+"}"),"} "+b.close).replace(/\\/g,"\\\\").replace(/(?="|‘)/g,"\\").replace(c.query(),function(a){return a=a.replace(h,"").replace(i,""),‘";‘+a.replace(/\\/g,"")+‘; view+="‘}).replace(c.query(1),function(a){var c=‘"+(‘;return a.replace(/\s/g,"")===b.open+b.close?"":(a=a.replace(d(b.open+"|"+b.close),""),/^=/.test(a)&&(a=a.replace(/^=/,""),c=‘"+_escape_(‘),c+a.replace(/\\/g,"")+‘)+"‘)}),a=‘"use strict";var view = "‘+a+‘";return view;‘;try{return f.cache=a=new Function("d, _escape_",a),a(e,c.escape)}catch(j){return delete f.cache,c.error(j,g)}},e.pt.render=function(a,b){var e,d=this;return a?(e=d.cache?d.cache(a,c.escape):d.parse(d.tpl,a),b?(b(e),void 0):e):c.error("no data")},f=function(a){return"string"!=typeof a?c.error("Template not found"):new e(a)},f.config=function(a){a=a||{};for(var c in a)b[c]=a[c]},f.v="1.1","function"==typeof define?define(function(){return f}):"undefined"!=typeof exports?module.exports=f:window.laytpl=f}();
或者下载地址:laytpl.js
1.引用该js
2.建立<script type="text/html"></script>的模板
3.用laytpl.js吧得到的数据加入到<script type="text/html"></script>模板
4.把模板渲染到你要的html的demo
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 5 <title></title> 6 <meta charset="utf-8" /> 7 <script src="/laytpl/laytpl.js"></script> 8 <script> 9 //假设你得到了这么一段数据 10 var data = { 11 title: ‘前端圈‘, 12 intro: ‘一群码js的骚年,幻想改变世界,却被世界改变。‘, 13 list: [{ name: ‘贤心‘, city: ‘杭州‘ }, { name: ‘谢亮‘, city: ‘北京‘ }, { name: ‘浅浅‘, city: ‘杭州‘ }, { name: ‘Dem‘, city: ‘北京‘ }] 14 }; 15 16 window.onload = function () { 17 //建立视图。用于呈现渲染结果。 18 var gettpl = document.getElementById(‘testTpl‘).innerHTML; 19 laytpl(gettpl).render(data, function (html) { 20 //得到的模板渲染到html 21 document.getElementById(‘testDiv‘).innerHTML = html; 22 }); 23 } 24 </script> 25 </head> 26 <body> 27 <script id="testTpl" type="text/html"> 28 <h1>{{ d.title }}</h1> 29 <p>{{d.intro}}</p> 30 <ul> 31 {{# for(var i = 0; i < d.list.length; i++){ }} 32 <li> 33 <span>姓名:{{ d.list[i].name }}</span> 34 <span>城市:{{ d.list[i].city }}</span> 35 </li> 36 {{# } }} 37 </ul> 38 </script> 39 <div id="testDiv"></div> 40 </body> 41 </html>
显示结果:
用laytpl获取模板的另一种写法:
var tpl = laytpl(gettpl);
tpl.render(data, function(result){
console.log(result);
document.getElementById("testDiv2").innerHTML = result;
});
以下是官方文档:
//第一步:编写模版。你可以使用一个script标签存放模板,如: <script id="demo" type="text/html"> <h1>{{ d.title }}</h1> <ul> {{# for(var i = 0, len = d.list.length; i < len; i++){ }} <li> <span>姓名:{{ d.list[i].name }}</span> <span>城市:{{ d.list[i].city }}</span> </li> {{# } }} </ul> </script> //第二步:建立视图。用于呈现渲染结果。 <div id="view"></div> //第三步:渲染模版 var data = { title: ‘前端攻城师‘, list: [{name: ‘贤心‘, city: ‘杭州‘}, {name: ‘谢亮‘, city: ‘北京‘}, {name: ‘浅浅‘, city: ‘杭州‘}, {name: ‘Dem‘, city: ‘北京‘}] }; var gettpl = document.getElementById(‘demo‘).innerHTML; laytpl(gettpl).render(data, function(html){ document.getElementById(‘view‘).innerHTML = html; });
一、模版语法 输出一个普通字段,不转义html: {{ d.field }} 输出一个普通字段,并转义html: {{= d.field }} JavaScript脚本: {{# JavaScript statement }} 二、内置方法 1):laytpl(template); //核心函数,返回一个对象 var tpl = laytpl(template); tpl.render(data, callback); //渲染方法,返回渲染结果,支持异步和同步两种模式 a):异步 tpl.render(data, function(result){ console.log(result); }); b):同步 var result = tpl.render(data); console.log(result); 2):laytpl.config(options); //初始化配置 options是一个对象 {open: ‘开始标签‘, close: ‘闭合标签‘} 3):laytpl.v //获取版本号
希望可以帮助到大家。
时间: 2024-10-12 20:00:53