Giter Site home page Giter Site logo

tpl's Introduction

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

语法

模板数据及工具方法

模板中的数据通过$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>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.