如何解决Tera 模板未加载上下文
我用 Rocket 框架在 Rust 中编写了一个简短的片段,并尝试使用 Tera 模板。
在评估模板时,上下文似乎没有正确加载。
这是 main.rs
文件的最小版本:
#[get("/")]
fn index() -> Template {
let mut context = Context::new();
context.insert("testArray",&[1,2,3,4]);
Template::render("index",&context)
}
fn main() {
rocket::ignite()
.mount("/",routes![index])
.attach(Template::fairing())
.launch();
}
我想遍历 testArray,但 tera 模板无法加载
错误 Error: Variable testArray not found in context while rendering 'index'
。我打印了上下文:
<div>
{{ __tera_context }}
</div>
并得到{}
。这种行为的原因可能是什么?我对 Rust 还很陌生,所以我想
我这边可能对语法有一些误解。我的代码片段基于 rocket repo examples。非常感谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。