65个版本
使用旧的Rust 2015
0.29.0 | 2020年1月26日 |
---|---|
0.28.0 | 2019年7月13日 |
0.27.0 | 2018年8月20日 |
0.26.2 | 2018年2月22日 |
0.6.1 | 2015年3月18日 |
#245 in HTTP服务器
每月263次下载
在 4 crates 中使用
21KB
322 行
handlebars-iron
Handlebars 中间件,用于 Iron Web框架。
此库与handlebars、iron和hyper一起工作,适用于稳定版和nightly rust。
Iron和Handlebars在0.x版本发布期间都有不兼容的更改。因此,您需要根据所使用的这两个版本选择handlebars-iron版本
handlebars-iron | handlebars | iron |
---|---|---|
0.14.x | 0.16.x | 0.2.x |
0.15.x | 0.18.x | 0.3.x |
0.16.0 | 0.19.x | 0.3.x |
0.17.x | 0.19.x | 0.4.x |
0.18.x | 0.20.x (serde 0.8) | 0.4.x |
0.19.x | 0.22.x | 0.4.x |
0.20.x | 0.23.x | 0.4.x |
0.21.x | 0.24.x | 0.4.x |
0.22.x | 0.24.x | 0.5.x |
0.23.x | 0.25.x (serde 0.9) | 0.5.x |
0.24.x | 0.26.x (serde 1.0) | 0.5.x |
0.25.x | 0.29.x | 0.5.x |
0.26.x | 0.32.x | 0.6.x |
0.27.x | 1.x | 0.6.x |
0.28.x | 2.x | 0.6.x |
0.29.x | 3.x | 0.6.x |
用法
将HandlebarsEngine添加到Iron中间件链中作为"after"中间件。
/// HandlebarsEngine will look up all files with "./examples/templates/**/*.hbs"
let mut hbse = HandlebarsEngine::new();
hbse.add(Box::new(DirectorySource::new("./examples/templates/", ".hbs")));
// load templates from all registered sources
if let Err(r) = hbse.reload() {
panic!("{}", r);
}
chain.link_after(hbse);
如果您想注册自己的自定义助手,可以从自定义的 Handlebars
注册表中初始化 HandlebarsEngine
。
let mut hbse = HandlebarsEngine::new();
hbse.add(Box::new(DirectorySource::new("./examples/templates/", ".hbs")));
hbse.handlebars_mut().register_helper("helper", my_helper);
// load templates from all registered sources
if let Err(r) = hbse.reload() {
panic!("{}", r);
}
chain.link_after(hbse);
有关自定义助手的更多信息,请参阅 handlebars-rust文档。
在您的处理器中,将 Template
设置为响应。根据Handlebars-rust的要求,您的数据应实现 serde::Serialize
。
对于 DirectorySource
,handlebars引擎将遍历由 prefix
指定的目录,尝试注册所有与后缀匹配的模板,并从中提取名称作为模板名称。例如, ./examples/templates/some/path/index.hbs
将注册为 some/path/index
。
/// render data with "index" template
/// that is "./examples/templates/index.hbs"
fn hello_world(_: &mut Request) -> IronResult<Response> {
let mut resp = Response::new();
let data = ...
resp.set_mut(Template::new("index", data)).set_mut(status::Ok);
Ok(resp)
}
通过使用 Template::with
,您还可以渲染一些模板而无需实际注册它。但这种方法不推荐,因为模板字符串需要每次都进行解析。如果可能,请考虑使用 MemorySource
。
/// render data with "index" template
/// that is "./examples/templates/index.hbs"
fn hello_world(_: &mut Request) -> IronResult<Response> {
let mut resp = Response::new();
let data = ...
resp.set_mut(Template::with("<h1>{{title}}</h1>", data)).set_mut(status::Ok);
Ok(resp)
}
由于这是一个简单的库,您可以首先使用 此示例,并通过 RUST_LOG=handlebars_iron=info cargo run --example server
运行,然后查看 文档。
Rust及其生态系统仍处于早期阶段,该项目可能因各种原因而出现故障。在1.0最终发布之前,我会尽力保持此库与最新的Rust nightly版本兼容。如果您发现任何问题,欢迎提交pull请求和问题报告。
实时重新加载
在开发过程中,您可能希望在不重新启动Web服务器的情况下实时重新加载模板。这就是实时重新加载功能的作用。
由于实时重新加载可能仅在开发阶段有用,我们将其作为一个可选功能。为了启用它,您需要在您的cargo声明中添加特征 watch
。
[features]
## create a feature in your app
watch = ["handlebars-iron/watch"]
[dependencies]
handlebars-iron = ...
查看 examples/watch_server.rs
获取更多信息。要测试它:RUST_LOG=handlebars_iron=info cargo run --example watch_server --features watch
。
使用handlebars-iron吗?
将您的项目添加到我们的 采用者 中。
许可证
当然,MIT。
依赖项
~7–16MB
~240K SLoC