diff --git a/index.js b/index.js index 9f70db9..e7a17c2 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ 'use strict' require('es.shim') + const Tool = require('./lib/tool') const path = require('path') @@ -22,17 +23,19 @@ class Smarty { } this.__REG__ = new RegExp(this.opt.ext + '$') - this.tool = new Tool() + this.tool = new Tool(this.opt) this.__DATA__ = Object.create(null) // 预定义的变量储存 - this.__CACHE__ = Object.create(null) // 模块缓存 + this.__TMP__ = Object.create(null) // 模板缓存 + this.__CACHE__ = Object.create(null) // 渲染缓存(模板被解析后) } config(key, val) { key += '' - if (!key || !val) { + if (!key || val === undefined) { return } this.opt[key] = val + this.tool.opt[key] = val } /** @@ -58,7 +61,7 @@ class Smarty { */ render(tpl = '', noParse = false) { var key = null - if (!this.tool.opt.path) { + if (!this.opt.path) { throw new Error('Smarty engine must define path option') } if (!tpl) { @@ -72,17 +75,20 @@ class Smarty { key = hash(tpl) - if (this.opt.cache && this.__CACHE__[key]) { + if (this.__CACHE__[key]) { return Promise.resolve(this.__CACHE__[key]) } - this.__CACHE__[key] = this.tool.__tpl__(tpl, noParse) + this.__TMP__[key] = this.tool.__readFile__(tpl, noParse) + if (noParse) { - return this.__CACHE__[key] + this.__CACHE__[key] = this.__TMP__[key] + delete this.__TMP__[key] + return Promise.resolve(this.__CACHE__[key]) } try { - this.__CACHE__[key] = this.tool.parse(this.__CACHE__[key], this.__DATA__) + this.__CACHE__[key] = this.tool.parse(this.__TMP__[key], this.__DATA__) return Promise.resolve(this.__CACHE__[key]) } catch (err) { return Promise.reject(err) diff --git a/lib/tool.js b/lib/tool.js index c62a97b..0397c7a 100644 --- a/lib/tool.js +++ b/lib/tool.js @@ -8,9 +8,10 @@ 'use strict' const fs = require('iofs') +const path = require('path') class Tool { - constructor() { + constructor(opt) { this.opt = { delimiter: [''], //模板界定符 labels: { @@ -31,6 +32,10 @@ class Tool { } } + Object.assign(this.opt, opt) + + this.__REG__ = new RegExp(this.opt.ext + '$') + //过滤器 this.filters = { html: function(str = '') { @@ -67,7 +72,7 @@ class Tool { } } - __tpl__(file) { + __readFile__(file, noParse) { var buf = null if (!fs.exists(file)) { throw new Error(`Can not find template "${file}"`) @@ -91,8 +96,8 @@ class Tool { //生成模板标签 __label__(id) { - let opt = this.opt - let tag = opt.labels[id] + var opt = this.opt + var tag = opt.labels[id] return this.__exp__(opt.delimiter[0] + tag + opt.delimiter[1]) } @@ -213,17 +218,18 @@ class Tool { matchInclude(m) { let begin = this.__exp__('^' + this.opt.delimiter[0] + 'include\\s+') let end = this.__exp__(this.opt.delimiter[1] + '$') + var tpl = '' m = m .replace(begin, '') .replace(end, '') .replace(/^['"]/, '') .replace(/['"]$/, '') - .replace(/\.tpl$/, '') //去掉可能出现的自带的模板后缀 + .replace(this.__REG__, '') //去掉可能出现的自带的模板后缀 - m += '.tpl' //统一加上后缀 + m += this.opt.ext //统一加上后缀 - let tpl = this.__tpl__(m) + tpl = this.__readFile__(path.resolve(this.opt.path, m)) //递归解析include tpl = tpl.replace(this.__label__('inc'), m1 => { return this.matchInclude(m1) @@ -274,7 +280,7 @@ class Tool { // 解析extends标签 parseExtends(str) { - let matches = str.match(/^/) + let matches = str.match(/^/) if (!matches) { str = str .replace(this.__label__('blockL'), '') @@ -293,25 +299,27 @@ class Tool { str = matches[1] .replace(/^['"]/, '') .replace(/['"]$/, '') - .replace(/\.tpl$/, '') //去掉可能出现的自带的模板后缀 + .replace(this.__REG__, '') //去掉可能出现的自带的模板后缀 - str += '.tpl' //统一加上后缀 + str += this.opt.ext //统一加上后缀 - str = this.__tpl__(str).replace(this.__label__('blockL'), (m, flag) => { - flag = flag.trim() - return blocks[flag] || '' - }) - blocks = undefined + str = this.__readFile__(path.resolve(this.opt.path, str)).replace( + this.__label__('blockL'), + (m, flag) => { + flag = flag.trim() + return blocks[flag] || '' + } + ) } return str } //解析模板 parse(str, data) { - this.vars = `"use strict"; let __filters__ = f; ` + var vars = `"use strict"; let __filters__ = f; ` for (let i in data) { let tmp = JSON.stringify(data[i]) || '' - this.vars += `let ${i} = ${tmp}; ` + vars += `let ${i} = ${tmp}; ` } str = str .trim() @@ -322,7 +330,7 @@ class Tool { str = this.parseExtends(str) str = this.parseNormal(str) - str = `${this.vars} let tpl=\`${str}\`; return tpl;` + str = `${vars} let tpl=\`${str}\`; return tpl;` return new Function('f', str)(this.filters) } diff --git a/package.json b/package.json index 4bee1fa..afcaa72 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "smartyx", - "version": "1.2.0", + "version": "1.3.0", "description": "nodeJS模板引擎,理念源自于PHP的smarty模板引擎", "keywords": ["fivejs", "smarty", "template", "ejs", "jade"], "author": "宇天 ",