velocityjs
Advanced tools
Comparing version 0.3.2 to 0.3.3
{ | ||
"name": "velocityjs", | ||
"description": "Velocity Template Language(VTL) for JavaScript", | ||
"version": "0.3.2", | ||
"version": "0.3.3", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "velocity template" |
##Velocity - Template Engine [![Build Status](https://secure.travis-ci.org/shepherdwind/velocity.js.png)](https://travis-ci.org/shepherdwind/velocity.js) | ||
*注意* | ||
velocityjs在npm中包名从原来的`velocity.js`改为`velocityjs`,感觉`require("velocity.js")` | ||
比较不好看,所以改名,从`0.3.0`之后的版本都在`velocityjs`下更新。 | ||
查看最新版本 | ||
```bash | ||
$ npm info velocityjs version | ||
``` | ||
velocity.js是velocity模板语法的javascript实现。 | ||
@@ -4,0 +15,0 @@ [Velocity](http://velocity.apache.org/) 是基于Java的模板引擎,广泛应用在阿里集 |
@@ -115,18 +115,28 @@ module.exports = function(Velocity, utils, BLOCK_TYPES){ | ||
var macro = this.macros[ast.id]; | ||
if (macro === undefined) return; | ||
if (macro === undefined){ | ||
macro = this.fns.macros[ast.id]; | ||
if (macro && macro.apply) { | ||
var _arg = []; | ||
utils.forEach(ast.args, function(arg){ | ||
_arg.push(arg.value); | ||
}); | ||
macro.apply(this, _arg); | ||
} | ||
} else { | ||
var guid = utils.guid(); | ||
var contextId = 'macro:' + guid; | ||
var local = { | ||
type: 'macro', | ||
variable: this._getArgus(macro.args), | ||
maps: ast.args, | ||
context: {} | ||
}; | ||
var guid = utils.guid(); | ||
var contextId = 'macro:' + guid; | ||
var local = { | ||
type: 'macro', | ||
variable: this._getArgus(macro.args), | ||
maps: ast.args, | ||
context: {} | ||
}; | ||
this.local[contextId] = local; | ||
this.conditions.push(contextId); | ||
this._render(macro.asts); | ||
this.local[contextId] = local; | ||
this.conditions.push(contextId); | ||
this._render(macro.asts); | ||
this.conditions.pop(); | ||
this.conditions.pop(); | ||
} | ||
}, | ||
@@ -133,0 +143,0 @@ |
@@ -15,4 +15,9 @@ var utils = require('../../utils'); | ||
init: function(asts){ | ||
init: function(asts, context, macros){ | ||
this.fns = { | ||
context: context || {}, | ||
macros: macros || {} | ||
}; | ||
this.context = {}; | ||
@@ -19,0 +24,0 @@ this.leafs = []; |
@@ -154,2 +154,13 @@ var Parser = require('../../parse/index'); | ||
return this._callMacro(macro, vmText); | ||
}, | ||
eval: function(str){ | ||
if (str) { | ||
var asts = Parser.parse(str); | ||
if (this instanceof Velocity){ | ||
this._render(asts); | ||
} else { | ||
throw new Error('不能改变this指向'); | ||
} | ||
} | ||
} | ||
@@ -156,0 +167,0 @@ |
@@ -104,16 +104,45 @@ module.exports = function(Velocity, utils){ | ||
isFn: function(ast){ | ||
var fn = this.fns.context; | ||
var isNoPass = true; | ||
fn = fn[ast.id]; | ||
if (fn) { | ||
var args = null; | ||
isNoPass = utils.some(ast.path, function(a){ | ||
fn = fn[a.id]; | ||
args = a.args; | ||
return !fn; | ||
}); | ||
var _arg = []; | ||
utils.forEach(args, function(arg){ | ||
_arg.push(arg.value); | ||
}); | ||
fn.apply(this, _arg); | ||
} | ||
return !isNoPass; | ||
}, | ||
getReferences: function(ast){ | ||
var astType = this.getRefType(ast); | ||
if (this.isFn(ast)) { | ||
var real = astType.real; | ||
var text = real.from ? this.getRefType(real.from): this.getRefText(real); | ||
} else { | ||
//执行过一遍,不再执行 | ||
if (this.cache[text]) return; | ||
var astType = this.getRefType(ast); | ||
this.toBasicType(astType); | ||
var real = astType.real; | ||
var text = real.from ? this.getRefType(real.from): this.getRefText(real); | ||
this.cache[text] = true; | ||
//执行过一遍,不再执行 | ||
if (this.cache[text]) return; | ||
this.toBasicType(astType); | ||
this.cache[text] = true; | ||
} | ||
}, | ||
@@ -120,0 +149,0 @@ |
@@ -15,3 +15,4 @@ var Parser = require('./parse/'); | ||
Compile : Compile, | ||
Helper : Helper | ||
Helper : Helper, | ||
Jsonify : Helper.Jsonify | ||
}; | ||
@@ -18,0 +19,0 @@ |
403463
3396
250