增加不解析的参数

master
宇天 2020-04-06 23:33:47 +08:00
parent 80e033d85b
commit f1a5c207d7
4 changed files with 36 additions and 46 deletions

View File

@ -8,18 +8,22 @@
require('es.shim') require('es.shim')
const Tool = require('./lib/tool') const Tool = require('./lib/tool')
const md5 = require('./lib/md5')
function hash(str) {
return Buffer.from(str).toString('hex')
}
class Smarty { class Smarty {
constructor(opt) { constructor(opt) {
this.opt = { cache: true } this.opt = { cache: true, ext: '.tpl' }
if (opt) { if (opt) {
Object.assign(this.opt, opt) Object.assign(this.opt, opt)
} }
this.__REG__ = new RegExp(this.opt.ext + '$')
this.tool = new Tool(this.opt) this.tool = new Tool(this.opt)
this.data = {} // 预定义的变量储存 this.__DATA__ = Object.create(null) // 预定义的变量储存
this.cache = {} // 模块缓存 this.__CACHE__ = Object.create(null) // 模块缓存
} }
config(key, val) { config(key, val) {
@ -37,7 +41,7 @@ class Smarty {
return this return this
} }
this.data[key] = val this.__DATA__[key] = val
return this return this
} }
@ -45,32 +49,36 @@ class Smarty {
* [render 模板渲染] * [render 模板渲染]
* @param {String} tpl 模板路径 * @param {String} tpl 模板路径
* @param {String} uuid 唯一标识 * @param {String} uuid 唯一标识
* @param {Boolean} noParse 不解析直接读取
* @return {Promise} 返回一个Promise对象 * @return {Promise} 返回一个Promise对象
*/ */
render(tpl = '', uuid = '') { render(tpl = '', uuid = '', noParse = false) {
var key = null
if (!this.tool.opt.path) { if (!this.tool.opt.path) {
console.log(this.tool)
throw new Error('Smarty engine must define path option') throw new Error('Smarty engine must define path option')
} }
if (!tpl) { if (!tpl) {
return Promise.reject('argument[tpl] can not be empty') return Promise.reject('argument[tpl] can not be empty')
} }
if (!/\.tpl$/.test(tpl)) { if (!this.__REG__.test(tpl)) {
tpl += '.tpl' tpl += this.opt.ext
} }
let cacheId = md5(tpl + uuid) key = hash(tpl + uuid)
if (this.opt.cache && this.cache[cacheId]) { if (this.opt.cache && this.__CACHE__[key]) {
return Promise.resolve(this.cache[cacheId]) 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 { try {
this.cache[cacheId] = this.tool.parse(this.cache[cacheId], this.data) this.__CACHE__[key] = this.tool.parse(this.__CACHE__[key], this.__DATA__)
return Promise.resolve(this.cache[cacheId]) return Promise.resolve(this.__CACHE__[key])
} catch (err) { } catch (err) {
return Promise.reject(err) return Promise.reject(err)
} }

View File

@ -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')
}

View File

@ -71,14 +71,19 @@ class Tool {
} }
__tpl__(name) { __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)) { if (!fs.exists(file)) {
throw new Error(`Can not find template "${file}"`) throw new Error(`Can not find template "${file}"`)
} }
return fs buf = fs.cat(file).toString()
.cat(file)
.toString() if (noParse) {
return buf
}
return buf
.replace(/[\r\n\t]+/g, ' ') //去掉所有的换行/制表 .replace(/[\r\n\t]+/g, ' ') //去掉所有的换行/制表
.replace(/\\/g, '\\\\') .replace(/\\/g, '\\\\')
} }

View File

@ -1,22 +1,16 @@
{ {
"name": "smartyx", "name": "smartyx",
"version": "1.0.3", "version": "1.1.0",
"description": "nodeJS模板引擎理念源自于PHP的smarty模板引擎", "description": "nodeJS模板引擎理念源自于PHP的smarty模板引擎",
"keywords": [ "keywords": ["fivejs", "smarty", "template", "ejs", "jade"],
"fivejs",
"smarty",
"template",
"ejs",
"jade"
],
"author": "宇天 <yutent@doui.cc>", "author": "宇天 <yutent@doui.cc>",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/yutent/smarty.git" "url": "https://github.com/yutent/smarty.git"
}, },
"dependencies": { "dependencies": {
"es.shim": "^1.0.1", "es.shim": "^1.1.2",
"iofs": "^1.1.0" "iofs": "^1.3.2"
}, },
"devDependencies": {}, "devDependencies": {},
"main": "lib/main.js", "main": "lib/main.js",