Skip to content

Commit

Permalink
Merge branch 'v4.9.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
aui committed May 11, 2017
2 parents 10d78de + 7f524fd commit 2a5056f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## v4.9.1

1. 修复模板内部 `$escape``$each` 变量可能没有定义的问题 [#3](https://github.com/aui/express-art-template/issues/3) [#1](https://github.com/aui/express-art-template/issues/1)

## v4.9.0

1. 增强调试功能:日志输出错误行号以及上下文
Expand Down
20 changes: 11 additions & 9 deletions lib/compile/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ var IMPORTS = '$imports';
/** $imports.$escape */
var ESCAPE = '$escape';

/** $imports.$each */
var EACH = '$each';

/** 文本输出函数 */
var PRINT = 'print';

Expand Down Expand Up @@ -53,8 +56,7 @@ var Compiler = function () {
* @param {Object} options
*/
function Compiler(options) {
var _external,
_internal,
var _internal,
_dependencies,
_this = this;

Expand All @@ -79,8 +81,8 @@ var Compiler = function () {
// context map
this.CONTEXT_MAP = {};

// 外部变量名单
this.external = (_external = {}, _external[DATA] = true, _external[IMPORTS] = true, _external[OPTIONS] = true, _external);
// 忽略的变量名单
this.ignore = [DATA, IMPORTS, OPTIONS].concat(options.ignore);

// 按需编译到模板渲染函数的内置变量
this.internal = (_internal = {}, _internal[OUT] = '\'\'', _internal[LINE] = '[0,0]', _internal[BLOCKS] = 'arguments[1]||{}', _internal[FROM] = 'null', _internal[PRINT] = 'function(){' + OUT + '+=\'\'.concat.apply(\'\',arguments)}', _internal[INCLUDE] = 'function(src,data){' + OUT + '+=' + OPTIONS + '.include(src,data||' + DATA + ',arguments[2]||' + BLOCKS + ',' + OPTIONS + ')}', _internal[EXTEND] = 'function(from){' + FROM + '=from}', _internal[BLOCK] = 'function(name,callback){if(' + FROM + '){' + OUT + '=\'\';callback();' + BLOCKS + '[name]=' + OUT + '}else{if(typeof ' + BLOCKS + '[name]===\'string\'){' + OUT + '+=' + BLOCKS + '[name]}else{callback()}}}', _internal);
Expand Down Expand Up @@ -168,14 +170,13 @@ var Compiler = function () {
var value = '';
var internal = this.internal;
var dependencies = this.dependencies;
var external = this.external;
var ignore = this.ignore;
var context = this.context;
var options = this.options;
var ignore = options.ignore;
var imports = options.imports;
var contextMap = this.CONTEXT_MAP;

if (!has(contextMap, name) && !has(external, name) && ignore.indexOf(name) < 0) {
if (!has(contextMap, name) && ignore.indexOf(name) === -1) {

if (has(internal, name)) {
value = internal[name];
Expand All @@ -187,7 +188,7 @@ var Compiler = function () {
}

// imports 继承了 Global,但是继承的属性不分配到顶级变量中,避免占用了模板内部的变量名称
} else if (has(imports, name)) {
} else if (name === ESCAPE || name === EACH || has(imports, name)) {
value = IMPORTS + '.' + name;
} else {
value = DATA + '.' + name;
Expand Down Expand Up @@ -448,7 +449,8 @@ Compiler.CONSTS = {
LINE: LINE,
BLOCKS: BLOCKS,
FROM: FROM,
ESCAPE: ESCAPE
ESCAPE: ESCAPE,
EACH: EACH
};

module.exports = Compiler;
4 changes: 2 additions & 2 deletions lib/template-web.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "art-template",
"description": "JavaScript Template Engine",
"homepage": "http://aui.github.com/art-template/",
"version": "4.9.0",
"version": "4.9.1",
"keywords": [
"template"
],
Expand Down
22 changes: 11 additions & 11 deletions src/compile/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const IMPORTS = `$imports`;
/** $imports.$escape */
const ESCAPE = `$escape`;

/** $imports.$each */
const EACH = `$each`;

/** 文本输出函数 */
const PRINT = `print`;

Expand Down Expand Up @@ -71,12 +74,8 @@ class Compiler {
// context map
this.CONTEXT_MAP = {};

// 外部变量名单
this.external = {
[DATA]: true,
[IMPORTS]: true,
[OPTIONS]: true
};
// 忽略的变量名单
this.ignore = [DATA, IMPORTS, OPTIONS, ...options.ignore];

// 按需编译到模板渲染函数的内置变量
this.internal = {
Expand Down Expand Up @@ -112,6 +111,7 @@ class Compiler {
} catch (error) {}
}


this.source = source;
this.getTplTokens(source, options.rules, this).forEach(tokens => {
if (tokens.type === tplTokenizer.TYPE_STRING) {
Expand Down Expand Up @@ -178,14 +178,13 @@ class Compiler {
let value = ``;
const internal = this.internal;
const dependencies = this.dependencies;
const external = this.external;
const ignore = this.ignore;
const context = this.context;
const options = this.options;
const ignore = options.ignore;
const imports = options.imports;
const contextMap = this.CONTEXT_MAP;

if (!has(contextMap, name) && !has(external, name) && ignore.indexOf(name) < 0) {
if (!has(contextMap, name) && ignore.indexOf(name) === -1) {

if (has(internal, name)) {
value = internal[name];
Expand All @@ -195,7 +194,7 @@ class Compiler {
}

// imports 继承了 Global,但是继承的属性不分配到顶级变量中,避免占用了模板内部的变量名称
} else if (has(imports, name)) {
} else if (name === ESCAPE || name === EACH || has(imports, name)) {
value = `${IMPORTS}.${name}`;
} else {
value = `${DATA}.${name}`;
Expand Down Expand Up @@ -473,7 +472,8 @@ Compiler.CONSTS = {
LINE,
BLOCKS,
FROM,
ESCAPE
ESCAPE,
EACH
};


Expand Down
13 changes: 13 additions & 0 deletions test/compile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,19 @@ module.exports = {
value: 'hello world'
});
assert.deepEqual('${value}hello world', html);
},

'imports': () => {
const render = compile('<%= $imports.stringify(value) %>', {
bail: true,
imports: {
stringify: JSON.stringify,
log: console.log
}
});
assert.deepEqual('&#34;hello&#34;', render({
value: 'hello'
}));
}
},

Expand Down

0 comments on commit 2a5056f

Please sign in to comment.