增加不解析的参数
parent
80e033d85b
commit
f1a5c207d7
38
index.js
38
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)
|
||||
}
|
||||
|
|
17
lib/md5.js
17
lib/md5.js
|
@ -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')
|
||||
}
|
13
lib/tool.js
13
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, '\\\\')
|
||||
}
|
||||
|
|
14
package.json
14
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": "宇天 <yutent@doui.cc>",
|
||||
"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",
|
||||
|
|
Loading…
Reference in New Issue