Nested loops cause data carrying forward
YaxinCheng opened this issue · comments
I found when I have a loop inside another, the data will be carried forward from one item to another. For example, I have a bunch of data: [
{'title': 'Swift', 'opt': [1, 2, 3], 'option': true}
{'title': 'Vapor', 'image': 'Vapor.png'}
]
If I do #loop(data, "each"), and inside the loop, I have #loop(each.opt, "opt"). It works for the first one, obviously. But it also works for the second one, because the opt from the first is carried forward to the second one for some reason... It has been bugging me for a while, and I am pretty sure my data is alright when I call drop.view.make().
Is there anyone who met the same or similar problem?
@YaxinCheng I'm not entirely sure what the specific issue is here, I know that we do our best to add some concept of "scope" to leaf, and this means outer scopes are often accessible, it sounds like you might be hitting some weirdness around it.
Can you give me a bit more from what your leaf snippet looks like, the data you're sending, what it's rendering, and what you'd like it to render?
Thanks so much, that would really help us get a test case going and get this patched now that we have some more time to develop Leaf 🙌
I have deleted the original code to make sure my web runs correctly, but I can give you an example from what I remember.
Data needs to be load:
[ {'type': 1, 'img': ['url1', 'url2'], 'subtitle': 'sub'}, {'type': 0, 'title': 't'} ]if I load the data by
#loop(data, 'each') {
type: #(each.type)
#if(each.title){
title: #(each.title)
}
#loop(each.img, 'img'){
#(img)
}
#if(each.subtitle){
subtitle: #(each.subtitle)
}
}
Expected output:
type: 1url1
url2
subtitle: sub
type: 0
title: t
Actual output:
type: 1url1
url2
subtitle: sub
type: 0
title: t
url1
url2
subtitle: sub
The data (img, subtitle) from previous item is carried forward to the next one. Hope this will help you locate the problem
@YaxinCheng thank you so much for this, that repro case was perfect and helped me track down the underlying issue in the parser not properly popping a context 👍
PR is up here #68