diff --git a/index.js b/index.js index e0dc4ca..ffeec52 100644 --- a/index.js +++ b/index.js @@ -8,18 +8,22 @@ require('es.shim') const Tool = require('./lib/tool') -const md5 = require('./lib/md5') + +function hash(str) { + return Buffer.from(str).toString('hex') +} class Smarty { constructor(opt) { - this.opt = { cache: true } + this.opt = { cache: true, ext: '.tpl' } if (opt) { Object.assign(this.opt, opt) } + this.__REG__ = new RegExp(this.opt.ext + '$') this.tool = new Tool(this.opt) - this.data = {} // 预定义的变量储存 - this.cache = {} // 模块缓存 + this.__DATA__ = Object.create(null) // 预定义的变量储存 + this.__CACHE__ = Object.create(null) // 模块缓存 } config(key, val) { @@ -37,7 +41,7 @@ class Smarty { return this } - this.data[key] = val + this.__DATA__[key] = val return this } @@ -45,32 +49,36 @@ class Smarty { * [render 模板渲染] * @param {String} tpl 模板路径 * @param {String} uuid 唯一标识 + * @param {Boolean} noParse 不解析直接读取 * @return {Promise} 返回一个Promise对象 */ - render(tpl = '', uuid = '') { + render(tpl = '', uuid = '', noParse = false) { + var key = null if (!this.tool.opt.path) { - console.log(this.tool) throw new Error('Smarty engine must define path option') } if (!tpl) { return Promise.reject('argument[tpl] can not be empty') } - if (!/\.tpl$/.test(tpl)) { - tpl += '.tpl' + if (!this.__REG__.test(tpl)) { + tpl += this.opt.ext } - let cacheId = md5(tpl + uuid) + key = hash(tpl + uuid) - if (this.opt.cache && this.cache[cacheId]) { - return Promise.resolve(this.cache[cacheId]) + if (this.opt.cache && this.__CACHE__[key]) { + return Promise.resolve(this.__CACHE__[key]) } - this.cache[cacheId] = this.tool.__tpl__(tpl) + this.__CACHE__[key] = this.tool.__tpl__(tpl, noParse) + if (noParse) { + return this.__CACHE__[key] + } try { - this.cache[cacheId] = this.tool.parse(this.cache[cacheId], this.data) - return Promise.resolve(this.cache[cacheId]) + this.__CACHE__[key] = this.tool.parse(this.__CACHE__[key], this.__DATA__) + return Promise.resolve(this.__CACHE__[key]) } catch (err) { return Promise.reject(err) } diff --git a/lib/md5.js b/lib/md5.js deleted file mode 100644 index ba4e4d9..0000000 --- a/lib/md5.js +++ /dev/null @@ -1,17 +0,0 @@ -/** - * - * @authors yutent (yutent@doui.cc) - * @date 2017-01-17 15:50:51 - * - */ - -'use strict' - -const crypto = require('crypto') - -module.exports = function(str = '') { - return crypto - .createHash('md5') - .update(str + '', 'utf8') - .digest('hex') -} diff --git a/lib/tool.js b/lib/tool.js index 54bfb89..ec31529 100644 --- a/lib/tool.js +++ b/lib/tool.js @@ -71,14 +71,19 @@ class Tool { } __tpl__(name) { - let file = path.resolve(this.opt.path, name) + var file = path.resolve(this.opt.path, name) + var buf = null if (!fs.exists(file)) { throw new Error(`Can not find template "${file}"`) } - return fs - .cat(file) - .toString() + buf = fs.cat(file).toString() + + if (noParse) { + return buf + } + + return buf .replace(/[\r\n\t]+/g, ' ') //去掉所有的换行/制表 .replace(/\\/g, '\\\\') } diff --git a/package.json b/package.json index 6bcd8c5..cc69d0b 100644 --- a/package.json +++ b/package.json @@ -1,22 +1,16 @@ { "name": "smartyx", - "version": "1.0.3", + "version": "1.1.0", "description": "nodeJS模板引擎,理念源自于PHP的smarty模板引擎", - "keywords": [ - "fivejs", - "smarty", - "template", - "ejs", - "jade" - ], + "keywords": ["fivejs", "smarty", "template", "ejs", "jade"], "author": "宇天 ", "repository": { "type": "git", "url": "https://github.com/yutent/smarty.git" }, "dependencies": { - "es.shim": "^1.0.1", - "iofs": "^1.1.0" + "es.shim": "^1.1.2", + "iofs": "^1.3.2" }, "devDependencies": {}, "main": "lib/main.js",