Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/steel1990/xtmpl
xtmpl is a js tempalte engine
https://github.com/steel1990/xtmpl
Last synced: about 2 months ago
JSON representation
xtmpl is a js tempalte engine
- Host: GitHub
- URL: https://github.com/steel1990/xtmpl
- Owner: steel1990
- Created: 2014-01-14T02:56:14.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2016-05-03T05:25:49.000Z (over 8 years ago)
- Last Synced: 2024-10-31T18:58:28.839Z (3 months ago)
- Language: JavaScript
- Size: 1.33 MB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
xtmpl
=====
[![Build Status](https://travis-ci.org/steel1990/xtmpl.png?branch=master)](https://travis-ci.org/steel1990/xtmpl)xtmpl 是一个简单的js模板引擎
demo: [http://steel1990.github.io/xtmpl](http://steel1990.github.io/xtmpl)
## 模板语法
### 简单插入xtmpl.compile('aa{{b}}cc')({ b: '' }); // aa<span>cc
// 对 html 不编码
xtmpl.compile('aa{{=b}}cc')({ b: '' }); // aacc
// 也可以使用全局配置,让其不编码
xtmpl.config('escapeHtml', false);
xtmpl.compile('aa{{b}}cc')({ b: '' }); // aacc// 可以使用 this
xtmpl.compile('aa{{=this}}cc')(''); // aacc### 逻辑判断
xtmpl.compile('if{{#if this}}xxx{{/if}}end')(0); // ifend
xtmpl.compile('if{{#if this}}xxx{{/if}}end')(1); // ifxxxend
xtmpl.compile('if{{#if !this}}xxx{{/if}}end')(0); // ifxxxend### 数组遍历
xtmpl.compile('{{#for list}}{{$value}}{{/for}}')({ list: [1, 2, 3] }); // 123
xtmpl.compile('{{#for this}}{{$value}}{{/for}}')([1, 2, 3]); // 123
xtmpl.compile('{{#for this}}{{#if $key}};{{/if}}{{$key}}:{{$value}}{{/for}}')([1, 2, 3]); // 0:1;1:2;2:3xtmpl.compile('{{#for list}}{{k}}{{/for}}')({
list: [{
k: 1
}, {
k: 2
}]
}); // 12### 对象遍历
xtmpl.compile('{{#forin obj}}{{$key}}:{{$value}};{{/forin}}')({
obj: {
a: 1,
b: 2,
c: 3
}
}); // a:1;b:2;c:3;### 注册行内接口
xtmpl.registerInlineHelper('enc', function(str) {
return encodeURIComponent(str);
});
xtmpl.compile('?a={{enc this}}')('?abc'); // ?a=%3Fabc
var data = {
a: 'p v',
b: '&&'
};
xtmpl.compile('?a={{enc a}}&b={{enc b}}')(data); // ?a=p%20v&b=%26%26