I have a Rust structure that looks something like this:
struct Root{
as: Vec<A>,
}
struct A {
bs: Vec<B>,
cs: Vec<C>,
}
struct B {
strings: Vec<String>,
}
struct C {
strings: Vec<u32>,
}
and i'm trying to get output using Rocket.rs and Handlebars templating.
My handlebars template currently looks like this, but it doesn't work.
{{#each as}}
{{#each bs}}
<h4>{{@index}}</h4>
<pre>{{bs.@index}}</pre>
<pre>{{cs.@index}}</pre>
{{/each}}
{{/each}}
I get the following error Error: Error rendering Handlebars template 'index' Error rendering "index" line 28, col 18: invalid digit found in string
, which probably has to do with the @index
variables I'm using in the HBS tags.
Is there any other way I could only get one item out of two arrays and place them side-by-side without having to alter my structure?
It is not clear to me what you are trying to achieve. It looks like that for each
A
Object in theas
Array that you would like to iterate over each element ofbs
andcs
. This assumes thatbs
andcs
have the same length for anyA
.If this is what you want then I think your problem is that you are trying to access a
cs
from within the context of abs
. Within the{{#each bs}}
block, the context is the currentB
Object. As aB
does not have acs
, you need to step-up a context level so that you return to the context of theA
which holds thebs
and thecs
. In Handlebars, you change the context using a path, like../
.A simplified template that accesses the
bs
and thecs
at each index ofbs
for eachA
would be:Note: I used the lookup helper for both the
bs
lookup and thecs
lookup for consistency. However, as we are within the context of thebs
, we could simply reference it with.
. As in:I have created a fiddle for your reference.