rust - 车把三个阵列在一个循环中
问题描述
我有一个看起来像这样的 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
,这可能与@index
我在 HBS 标签中使用的变量有关。
有没有其他方法我只能从两个数组中取出一个并将它们并排放置而不必改变我的结构?
解决方案
我不清楚你想要达到什么目的。A
对于 Array 中的每个Object as
,您希望遍历 和 的每个元素,bs
看起来就像这样cs
。这假设bs
和cs
具有相同的长度A
。
如果这是您想要的,那么我认为您的问题是您试图cs
从 a 的上下文中访问 a bs
。在{{#each bs}}
块内,上下文是当前B
对象。由于 aB
没有 a cs
,因此您需要提升上下文级别,以便返回A
包含bs
和的上下文cs
。在 Handlebars 中,您可以使用路径更改上下文../
,例如.
访问for each的每个索引的bs
和at的简化模板是:cs
bs
A
{{#each as}}
{{#each bs}}
<h4>{{@index}}</h4>
<pre>{{lookup ../bs @index}}</pre>
<pre>{{lookup ../cs @index}}</pre>
{{/each}}
{{/each}}
注意:我对查找和查找都使用了查找帮助程序以保持一致性。但是,由于我们在 的上下文中,我们可以简单地用 来引用它。如:bs
cs
bs
.
<pre>{{.}}</pre>
<pre>{{lookup ../cs @index}}</pre>
我创建了一个小提琴供您参考。
推荐阅读
- python - 如何在用户注册结束时填充模型
- python - Python写入HTML时如何修复编码错误
- javascript - 在vscode中自动完成材料元素?
- python-3.x - 如何确保在flask WTForms中,表单字段仅在填写后才进行有效性测试?
- java - 如何编写每个循环通过字符串数组处理来计算子字符串
- r - 循环内连续研究站点的相关图:如何将站点名称添加为主要标题?
- geoserver - 如何避免geoserver中图层组中2个图层的标签重叠?
- mongodb - 使用静态数据创建 REST API 的最佳工具
- c# - 为什么使用 Parallel.ForEach 时主线程被阻塞
- laravel - 从数据库中删除数据,“不支持 GET 方法”