velocityjs
Advanced tools
Comparing version 2.0.2 to 2.0.3
@@ -0,1 +1,5 @@ | ||
## 2.0.3 2020-12-02 | ||
- fix: issue with loop variable access inside nested foreach statements [136](https://github.com/shepherdwind/velocity.js/pull/136) | ||
## 2.0.2 2020-10-19 | ||
@@ -2,0 +6,0 @@ |
{ | ||
"name": "velocityjs", | ||
"description": "Velocity Template Language(VTL) for JavaScript", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -9,10 +9,12 @@ module.exports = function(Velocity, utils) { | ||
*/ | ||
getContext: function() { | ||
var condition = this.condition; | ||
getContext: function(idName) { | ||
var local = this.local; | ||
if (condition) { | ||
return local[condition]; | ||
} else { | ||
return this.context; | ||
// context find, from the conditions stack top to end | ||
for (var condition of this.conditions) { | ||
if (local[condition].hasOwnProperty(idName)) { | ||
return local[condition]; | ||
} | ||
} | ||
// not find local variable, return global context | ||
return this.context; | ||
}, | ||
@@ -24,3 +26,3 @@ /** | ||
var ref = ast.equal[0]; | ||
var context = this.getContext(); | ||
var context = this.getContext(ref.id); | ||
@@ -31,5 +33,2 @@ // @see #25 | ||
// fix #129 | ||
} else if (!context.hasOwnProperty(ref.id)) { | ||
// set var to global context, see #100 | ||
context = this.context; | ||
} | ||
@@ -36,0 +35,0 @@ |
@@ -219,2 +219,40 @@ var Velocity = require('../src/velocity') | ||
}) | ||
it('nested foreach subprop', function() { | ||
var tpl = ` | ||
#set($list = [{"prop": "a"}]) | ||
#set($list2 = ["a", "b", "c"]) | ||
#foreach($i in $list) | ||
#set($fc = $velocityCount - 1) | ||
#foreach($j in $list2) | ||
#set($i.prop = "$i.prop$j") | ||
#end | ||
#end | ||
$list | ||
` | ||
var ret = render(tpl).trim() | ||
assert.strictEqual('[{prop=aabc}]', ret); | ||
}) | ||
it('nested foreach set', function() { | ||
var tpl = ` | ||
#set($obj = [{ | ||
"SubProp": [ | ||
{ "SubSubProp": "a" } | ||
] | ||
}]) | ||
#set($subSubPropRealValue = "b") | ||
#foreach($sub in $obj) | ||
#set($fc = $velocityCount - 1) | ||
#foreach($subsub in $sub.SubProp) | ||
#set($fcc = $velocityCount - 1) | ||
#set($sub.SubProp[$fcc].SubSubProp = $subSubPropRealValue) | ||
#end | ||
#set($obj[$fc] = $sub) | ||
#end | ||
$obj | ||
` | ||
var ret = render(tpl).trim() | ||
assert.strictEqual('[{SubProp=[{SubSubProp=b}]}]', ret); | ||
}); | ||
}) |
178474
44
3777