【问题标题】:Handlebars three arrays in one loop车把三个阵列在一个循环中
【发布时间】:2021-01-26 12:25:11
【问题描述】:

我的 Rust 结构看起来像这样:

struct Root{
    as: Vec<A>,
}

struct A {
    bs: Vec<B>,
    cs: Vec<C>,
}

struct B {
    strings: Vec<String>,
}


struct C {
    strings: Vec<u32>,
}

我正在尝试使用 Rocket.rs 和 Handlebars 模板获取输出。

我的车把模板目前看起来像这样,但它不起作用。

{{#each as}}
    {{#each bs}}
        <h4>{{@index}}</h4>
        <pre>{{bs.@index}}</pre>
        <pre>{{cs.@index}}</pre>
    {{/each}}
{{/each}}

我收到以下错误Error: Error rendering Handlebars template 'index' Error rendering "index" line 28, col 18: invalid digit found in string,这可能与我在 HBS 标签中使用的@index 变量有关。

有没有其他方法可以只从两个数组中取出一个并将它们并排放置而无需改变我的结构?

【问题讨论】:

    标签: rust handlebars.js rust-rocket


    【解决方案1】:

    我不清楚你想要达到什么目的。对于as 数组中的每个A 对象,您希望遍历bscs 的每个元素,看起来像这样。这假定bscs 对于任何A 具有相同的长度。

    如果这是您想要的,那么我认为您的问题是您试图从bs 的上下文中访问cs。在{{#each bs}} 块内,上下文是当前的B 对象。由于B 没有cs,您需要提升上下文级别,以便返回到包含bscsA 的上下文。在 Handlebars 中,您 change the context 使用路径,例如 ../

    对于每个A,在bs 的每个索引处访问bscs 的简化模板是:

    {{#each as}}
        {{#each bs}}
            <h4>{{@index}}</h4>
            <pre>{{lookup ../bs @index}}</pre>
            <pre>{{lookup ../cs @index}}</pre>
        {{/each}}
    {{/each}}
    

    注意:我将lookup helper 用于bs 查找和cs 查找以保持一致性。然而,由于我们在bs 的上下文中,我们可以简单地用. 引用它。如:

    <pre>{{.}}</pre>
    <pre>{{lookup ../cs @index}}</pre>
    

    我创建了一个fiddle 供您参考。

    【讨论】:

    • 我什至不确定自己要达到什么目标,但您的回答清楚地表明这是可行的。我有三个相同大小的数组,并试图在表格的&lt;td&gt; 中并排显示它们。感谢您的回答和小提琴!
    猜你喜欢
    • 1970-01-01
    • 2019-02-07
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多