Skip to content

Commit

Permalink
# 0.6.0 / 2015-11-26
Browse files Browse the repository at this point in the history
- 大幅优化渲染速度,测试显示提速4倍以上
- 添加对输出变量未定义时的容错处理,不会在报错,而是返回空字符串
- 添加供预编译处理器使用的接口
  • Loading branch information
yanhaijing committed Nov 26, 2015
1 parent e8c934f commit bff0e52
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 19 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.6.0 / 2015-11-26
- 大幅优化渲染速度,测试显示提速4倍以上
- 添加对输出变量未定义时的容错处理,不会在报错,而是返回空字符串
- 添加供预编译处理器使用的接口

# 0.5.0 / 2015-11-20
- 支持语句省略分号,改用前置插入分号机制
- 编译输出只显示核心代码
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [template.js](https://github.com/yanhaijing/template.js) [![Build Status](https://travis-ci.org/yanhaijing/template.js.svg?branch=master)](https://travis-ci.org/yanhaijing/template.js) [![Built with Grunt](https://cdn.gruntjs.com/builtwith.png)](http://gruntjs.com/) [![release](https://img.shields.io/badge/release-v0.5.0-orange.svg)](https://github.com/yanhaijing/template.js/releases/tag/v0.5.0) [![spm package](http://spmjs.io/badge/template.js)](http://spmjs.io/package/template.js) [![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/yanhaijing/template.js/blob/master/MIT-LICENSE.txt)
# [template.js](https://github.com/yanhaijing/template.js) [![Build Status](https://travis-ci.org/yanhaijing/template.js.svg?branch=master)](https://travis-ci.org/yanhaijing/template.js) [![Built with Grunt](https://cdn.gruntjs.com/builtwith.png)](http://gruntjs.com/) [![release](https://img.shields.io/badge/release-v0.6.0-orange.svg)](https://github.com/yanhaijing/template.js/releases/tag/v0.6.0) [![spm package](http://spmjs.io/badge/template.js)](http://spmjs.io/package/template.js) [![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/yanhaijing/template.js/blob/master/MIT-LICENSE.txt)

template.js 一款javascript模板引擎,简单,好用。

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "template.js",
"main": "template.js",
"version": "0.5.0",
"version": "0.6.0",
"homepage": "https://github.com/yanhaijing/template.js",
"authors": [
"yanhaijing <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "template_js",
"description": "template.js 一款javascript模板引擎,简单,好用。",
"version": "0.5.0",
"version": "0.6.0",
"homepage": "https://github.com/yanhaijing/template.js",
"author": {
"name": "yanhaijing",
Expand Down
4 changes: 2 additions & 2 deletions template.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
* template.js v0.5.0 (https://github.com/yanhaijing/template.js)
* template.js v0.6.0 (https://github.com/yanhaijing/template.js)
* API https://github.com/yanhaijing/template.js/blob/master/doc/api.md
* Copyright 2015 yanhaijing. All Rights Reserved
* Licensed under MIT (https://github.com/yanhaijing/template.js/blob/master/MIT-LICENSE.txt)
Expand Down Expand Up @@ -251,6 +251,6 @@
template.__compress = compress;
template.__handelError = handelError;
template.__compile = compile;
template.version = '0.5.0';
template.version = '0.6.0';
return template;
}));
256 changes: 256 additions & 0 deletions test/js/template-0.6.0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
/*!
* template.js v0.6.0 (https://github.com/yanhaijing/template.js)
* API https://github.com/yanhaijing/template.js/blob/master/doc/api.md
* Copyright 2015 yanhaijing. All Rights Reserved
* Licensed under MIT (https://github.com/yanhaijing/template.js/blob/master/MIT-LICENSE.txt)
*/
;(function(root, factory) {
var template = factory(root);
if (typeof define === 'function' && define.amd) {
// AMD
define('template', function() {
return template;
});
} else if (typeof exports === 'object') {
// Node.js
module.exports = template;
} else {
// Browser globals
var _template = root.template;

template.noConflict = function() {
if (root.template === template) {
root.template = _template;
}

return template;
};
root.template = template;
}
}(this, function(root) {
'use strict';
var o = {
sTag: '<%',//开始标签
eTag: '%>',//结束标签
compress: false,//是否压缩html
escape: true, //默认输出是否进行HTML转义
error: function (e) {}//错误回调
};
var toString = {}.toString;

function getType(x) {
if(x === null){
return 'null';
}

var t= typeof x;

if(t !== 'object'){
return t;
}

var c = toString.call(x).slice(8, -1).toLowerCase();
if(c !== 'object'){
return c;
}

if(x.constructor==Object){
return c;
}

return 'unkonw';
}

function isObject(obj) {
return getType(obj) === 'object';
}
function extend() {
var target = arguments[0] || {};
var arrs = Array.prototype.slice.call(arguments, 1);
var len = arrs.length;

for (var i = 0; i < len; i++) {
var arr = arrs[i];
for (var name in arr) {
target[name] = arr[name];
}

}
return target;
}
function encodeHTML(source) {
return String(source)
.replace(/&/g,'&amp;')
.replace(/</g,'&lt;')
.replace(/>/g,'&gt;')
.replace(/\\/g,'&#92;')
.replace(/"/g,'&quot;')
.replace(/'/g,'&#39;');
};
function compress(html) {
return html.replace(/\s+/g, ' ').replace(/<!--[\w\W]*?-->/g, '');
}
function handelError(e) {
var message = 'template.js error\n\n';

for (var key in e) {
message += '<' + key + '>\n' + e[key] + '\n\n';
}
message += '<message>\n' + e.message + '\n\n';
console && console.error && console.error(message);

o.error(e);
function error() {
return 'template.js error';
}
error.toString = function () {
return '__str__ = "template.js error"';
}
return error;
}
function parse(tpl, opt) {
var code = '';
var sTag = opt.sTag;
var eTag = opt.eTag;
var escape = opt.escape;
function parsehtml(line) {
// 单双引号转义,换行符替换为空格
line = line.replace(/('|")/g, '\\$1').replace(/\n/g, ' ');
return ';__code__ += ("' + line + '")\n';
}
function parsejs(line) {
var html;
if (line.search(/^=/) !== -1) {
//默认输出
html = line.slice(1);
html = escape ? ('__encodeHTML__(typeof (' + html + ') === "undefined" ? "" : ' + html + ')') : html;
return ';__code__ += (' + html + ')\n';
}

if (line.search(/^:h=/) !== -1) {
//HTML转义输出
html = line.slice(3);
return ';__code__ += (__encodeHTML__(typeof (' + html + ') === "undefined" ? "" : ' + html + '))\n';
}

if (line.search(/^:=/) !== -1) {
//不转义
html = line.slice(2);
return ';__code__ += (typeof (' + html + ') === "undefined" ? "" : ' + html + ')\n';
}

if (line.search(/^:u=/) !== -1) {
//URL转义
html = line.slice(3);
return ';__code__ += (typeof (' + html + ') === "undefined" ? "" : encodeURI(' + html + '))\n';
}

//原生js
return ';' + line + '\n';
}

var tokens = tpl.split(sTag);

for (var i = 0, len = tokens.length; i < len; i++) {
var token = tokens[i].split(eTag);

if (token.length === 1) {
code += parsehtml(token[0]);
} else {
code += parsejs(token[0], true);
if (token[1]) {
code += parsehtml(token[1]);
}
}
}

return code;
}
function compiler(tpl, opt) {
var mainCode = parse(tpl, opt);

var headerCode = '\n' +
' var html = (function (__data__, __encodeHTML__) {\n' +
' var __str__ = "", __code__ = "";\n' +
' for(var key in __data__) {\n' +
' __str__+=("var " + key + "=__data__[\'" + key + "\'];");\n' +
' }\n' +
' eval(__str__);\n\n';

var footerCode = '\n' +
' ;return __code__;\n' +
' }(__data__, __encodeHTML__));\n' +
' return html;\n';

var code = headerCode + mainCode + footerCode;
code = code.replace(/[\r]/g, ' '); // ie 7 8 会报错,不知道为什么
try {
var Render = new Function('__data__', '__encodeHTML__', code);
Render.toString = function () {
return mainCode;
}
return Render;
} catch(e) {
e.temp = 'function anonymous(__data__, __encodeHTML__) {' + code + '}';
throw e;
}
}
function compile(tpl, opt) {
opt = extend({}, o, opt);

try {
var Render = compiler(tpl, opt);
} catch(e) {
e.name = 'CompileError';
e.tpl = tpl;
e.render = e.temp;
delete e.temp;
return handelError(e);
}

function render(data) {
try {
var html = Render(data, encodeHTML);
html = opt.compress ? compress(html) : html;
return html;
} catch(e) {
e.name = 'RenderError';
e.tpl = tpl;
e.render = Render.toString();
return handelError(e);
}
}

render.toString = function () {
return Render.toString();
};
return render;
}
function template(tpl, data) {
if (typeof tpl !== 'string') {
return '';
}

var fn = compile(tpl);
if (!isObject(data)) {
return fn;
}

return fn(data);
}

template.config = function (option) {
if (isObject(option)) {
o = extend(o, option);
}

return extend({}, o);
};

template.__encodeHTML = encodeHTML;
template.__compress = compress;
template.__handelError = handelError;
template.__compile = compile;
template.version = '0.6.0';
return template;
}));
11 changes: 11 additions & 0 deletions test/speed2.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<script src="js/highcharts.js"></script>

<script src="../template.js"></script>
<script src="js/template-0.6.0.js"></script>
<script src="js/template-0.5.0.js"></script>
<script src="js/template-0.4.0.js"></script>
<script src="js/template-0.3.1.js"></script>
Expand All @@ -20,6 +21,7 @@
var mytemplate031 = template.noConflict();
var mytemplate040 = template.noConflict();
var mytemplate050 = template.noConflict();
var mytemplate060 = template.noConflict();
var mytemplate = template.noConflict();

// 数据量
Expand Down Expand Up @@ -53,6 +55,15 @@
}
}
},
{
name: 'template.js v0.6.0',
tester: function () {
var template=mytemplate060;
for (var i = 0; i < number; i ++) {
var tpl = template(document.getElementById('baidu-template').innerHTML);
}
}
},
{
name: 'template.js v0.5.0',
tester: function () {
Expand Down
Loading

0 comments on commit bff0e52

Please sign in to comment.