6 Star 14 Fork 6

笔心 / tpl

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

##简单小巧,性能出众的js模板引擎,原生js语法,压缩版不到1k

演示地址

http://qinshenxue.oschina.io/tpl/

语法

模板数据及工具方法

传入模板中的数据参数名为$data 使用工具方法(API中说明了如何增加工具方法)通过$tools调用

输出属性值

<div> {{= $data.propName }} </div>
<div> {{= Math.random() }} </div>
<div> {{= $tools.formatDate($data.date) }} </div>

js原生语法

{{ if( $data.really){ }}
<div>do sth...</div>
{{ }else{ }}
<div>do sth...</div>
{{ } }}
{{ var list=$data.list; }}
{{ for(var i=0,j=list.length;i<j;i++){ }}
    <div>{{= list[i]}}</div>
{{ } }}

API

###tpl(tplId,data)

  • tplId 存放模板的容器id
  • data 渲染模板的数据 返回渲染结果
document.getElementById('output').innerHTML = tpl('tpl-example',{list:[1,2,3]});

###tpl.compile(source[,cacheId])

  • source 要编译的模板内容
  • [非必须]cacheId 缓存的id 返回渲染函数
var render=tpl.compile('{{=$data.prop}}');
render({prop:'hello world'});

###tpl.render(tplId,data) 同tpl(tplId,data)

tpl.tool(toolName,toolFunction) 添加工具方法,在模板中可以调用

  • toolName 工具方法名
  • toolFunction 工具方法
tpl.tool('formatDate', function (date) {
        var d = new Date(date);
        return d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
    });

模板内使用

<div> {{= $tools.formatDate($data.date) }} </div>
The MIT License (MIT) Copyright (c) 2016 笔心 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

简单小巧,性能出众的js模板引擎,原生js语法,压缩版不到1k 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/qinshenxue/tpl.git
git@gitee.com:qinshenxue/tpl.git
qinshenxue
tpl
tpl
master

搜索帮助

14c37bed 8189591 565d56ea 8189591