gpt4 book ai didi

rust - 火箭内的 Handlebars helper

转载 作者:行者123 更新时间:2023-12-03 11:45:05 24 4
gpt4 key购买 nike

我正在尝试为 Handlebars 注册一个辅助程序,最终目的是要有一个能够计算/打印Vec长度的辅助程序。但是我已经无法从文档中获取样本到我的程序中。我设法做了一个“最小”示例,该示例显示了与我的火箭页面相同的错误。

#[macro_use]
extern crate handlebars;

extern crate rocket;
extern crate rocket_contrib;

use handlebars::{Context, Handlebars, Helper, HelperResult, JsonRender, Output, RenderContext};
use rocket_contrib::templates::Template;

handlebars_helper!(hex: |v: i64| format!("0x{:x}", v));

fn wow_helper(
h: &Helper,
_: &Handlebars,
_: &Context,
_: &mut RenderContext,
out: &mut Output,
) -> HelperResult {
if let Some(param) = h.param(0) {
out.write("<b><i>")?;
out.write(&param.value().render())?;
out.write("</b></i>")?;
}

Ok(())
}

fn main() {
rocket::ignite()
.attach(Template::custom(|engines| {
engines
.handlebars
.register_helper("mlenb", Box::new(wow_helper));
}))
.launch();
println!("Hello, world!");
}
cargo.toml中的依赖项:
[dependencies]
handlebars="3.3.0"
rocket="0.4.5"

[dependencies.rocket_contrib]
version = "0.4.5"
default-features = false
features = ["diesel_sqlite_pool", "handlebars_templates", "serve"]
错误:
error[E0631]: type mismatch in function arguments
--> src/main.rs:184:43
|
162 | / fn wow_helper(
163 | | h: &handlebars::Helper,
164 | | _: &handlebars::Handlebars,
165 | | _: &handlebars::Context,
... |
175 | | Ok(())
176 | | }
| |_- found signature of `for<'r, 's, 't0, 't1, 't2, 't3, 't4, 't5, 't6, 't7> fn(&'r handlebars::Helper<'s, 't0>, &'t1 handlebars::Handlebars<'t2>, &'t3 handlebars::Context, &'t4 mut handlebars::RenderContext<'t5, 't6>, &'t7 mut (dyn handlebars::Output + 't7)) -> _`
...
184 | .register_helper("mlenb", Box::new(wow_helper));
| ^^^^^^^^^^^^^^^^^^^^ expected signature of `for<'r, 'reg, 'rc, 's, 't0> fn(&'r rocket_contrib::templates::handlebars::Helper<'reg, 'rc>, &'reg rocket_contrib::templates::handlebars::Handlebars, &'rc rocket_contrib::templates::handlebars::Context, &'s mut rocket_contrib::templates::handlebars::RenderContext<'reg>, &'t0 mut (dyn rocket_contrib::templates::handlebars::Output + 't0)) -> _`
|
= note: required because of the requirements on the impl of `rocket_contrib::templates::handlebars::HelperDef` for `for<'r, 's, 't0, 't1, 't2, 't3, 't4, 't5, 't6, 't7> fn(&'r handlebars::Helper<'s, 't0>, &'t1 handlebars::Handlebars<'t2>, &'t3 handlebars::Context, &'t4 mut handlebars::RenderContext<'t5, 't6>, &'t7 mut (dyn handlebars::Output + 't7)) -> std::result::Result<(), handlebars::RenderError> {wow_helper}`
= note: required for the cast to the object type `dyn rocket_contrib::templates::handlebars::HelperDef`

最佳答案

不需要在cargo.toml中将 crate Handlebars 与火箭 crate 一起使用。
它的工作原理如下:
仅在cargo.toml中

[dependencies]
rocket = "0.4.5"
[dependencies.rocket_contrib]
version = "0.4.5"
default-features = false
features = ["handlebars_templates", "serves"]
并在main.rs中使用:
use rocket_contrib :: templates :: {Template, handlebars};
use handlebars :: {Helper, Handlebars, Context, RenderContext, Output, HelperResult, RenderError};
因此,对 Handlebars 进行版本控制不会有任何问题!

关于rust - 火箭内的 Handlebars helper ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63216776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com