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 (将#修改为@)