reference to objects in benchpress
-
hi
I'm trying to write a template for a plugin with some data that looks like this:const data = { people:{ "a": [ {name:'john',id:1}, {name:'dave',id:2} ] "b": [ {name:'sam',id:1}, {name:'eli',id:2}, {name:'nicole',id:3} ] } } res.render('path/template-name', data)
my code in template is something like this:
{{{ each people}}} <div> <ul> {{{each people}}} <li> {people.name} </li> {{{end}}} </ul> </div> {{{ end }}}
when I render the template it does create two lists with 2 and 3 items but it doesn't show the names. I tried doing it with @value.name instead of people.name too but it just writes {[object Object].name}.
can anyone show me what I'm doing wrong?
@3rd-Party-Developers -
@shayan The structure of your data is incorrect.
people
should be an array, if you wish to use{{{ each }}}
with it.(Unless there's some crazy behaviour in benchpress that allows for iterating through objects...)
Your template would work if your data was structured like this:
const data = { people: [ { people: [ {name:'john',id:1}, {name:'dave',id:2} ] }, { people: [ {name:'sam',id:1}, {name:'eli',id:2}, {name:'nicole',id:3} ] }, ], };
However I would really not recommend naming both nested objects
people
. That is confusing -
(Unless there's some crazy behaviour in benchpress that allows for iterating through objects...)
there is an example in the documentation (https://github.com/benchpressjs/benchpressjs/blob/master/docs/iteration.md#example-2-1) which has a similar structure to my data but it is kind of vague on how should I Iterate and reference the values if every value of a key like
jumpbugger
is an array of objects.However I would really not recommend naming both nested objects
people
. That is confusingmaybe how I structured the data is not standard but my problem was that the nested objects (like the second and third
people
key in your structure) has a dynamic key (likea
andb
) and not a static one (likepeople
which you said) , so I couldn't iterate over it no matter what I did.
fortunately I can change the structure so tnx . -
@shayan what do you want the output to look like given that data?
Something like this might work:
{{{ each people}}} <div> <ul> {{{ each @value }}} <li> {./name} </li> {{{ end }}} </ul> </div> {{{ end }}}
-
@pitaj
I want to have a list for every key inpeople
object and populate that list with the names of people I have in array of every key, something like this:
fora
:- john
- dave
for
b
:- sam
- eli
- nicole
I did not mention that but I also tried using
./name
. it also didn't show any names, just an empty bullet.
the only way I could reference the object was with@value
but when I tried to access a value of a specific key (like{@value.name}
) it just wrote{[object Object].name}
instead of actual value -
@shayan
@value.name
wouldn't work, but./name
is equivalent to what you're trying to express.Did you try exactly what I suggested?
-
@shayan weird, because it worked when I tried it. Can you check what version of benchpress you have?
npm ls benchpressjs
-
@shayan woops actually it wasn't working for me. It looks like this is a bug. For some reason
./name
in the inner loop refers to thename
property of the value in the outer loop. I've created an issue:Iterating over `@value` results in incorrect path resolution 路 Issue #97 路 benchpressjs/benchpressjs
Describe the bug {{{ each people}}} {{{ each @value }}} {./name} In the above, ./name will try to resolve to people[i].name instead of people[i][j].name. Minimal Reproduction https://runkit.com/pitaj/5fecc2b6246e6e001bac76d9 Versions Nod...
GitHub (github.com)