handlebars-iron

Handlebars middleware for the Iron web framework.

Build Status Gitter

This library, together with handlebars, iron and hyper, works on both stable and nightly rust.

Both iron and handlebars has backward-incompatible change during 0.x releases. So you will need to choose handlebars-iron version based on those two versions you were using:

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

Usage

Add HandlebarsEngine to your Iron middleware chain as an "after" middleware.

  /// 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);

If you want register your own custom helpers, you can initialize the HandlebarsEngine from a custom Handlebars registry.

  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);

You can find more information about custom helper in handlebars-rust document.

In your handler, set Template to response. As required by Handlebars-rust, your data should impl serialize::json::ToJson. If you are on nightly channel, it is highly recommended to use tojson_macros to generate default ToJson implementation without repeating yourself.

For DirectorySource, handlebars engine will walk the directory specified by prefix, try to register all templates matches the suffix, and extract its name as template name. For instance, ./examples/templates/some/path/index.hbs will be registered as 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)
}

By using Template::with You can also render some template without actually register it. But this is not recommended because template string needs to be parsed every time. Consider using a MemorySource if possible.

/// 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)
}

Since this is simple library, you may run this example with RUST_LOG=handlebars_iron=info cargo run --example server first, and documentation then.

Rust and its ecosystem are still in early stage, this project might been broken for various reasons. I will try my best to keep this library compiles with latest Rust nightly before the 1.0 final release. If you find anything bad, pull requests and issue reporting are always welcomed.

Live reload

During development you may want to live-reload your templates without having to restart your web server. Here comes the live-reload feature.

Since live-reload may only be useful in development phase, we have made it a optional feature. In order to enable it, you will need to add feature watch in your cargo declaration:

[features]
## create a feature in your app
watch = ["handlebars-iron/watch"]

[dependencies]
handlebars-iron = ...

Check examples/watch_server.rs for further information. To test it: RUST_LOG=handlebars_iron=info cargo run --example watch_server --features watch.

Serde

From 0.11.0, by enabling feature serde_type, you can also use serde_json as type system. Typically you can annotate your type with #[derive(Serialize)] to make it work as template data.

You can find an working example in examples/server.rs and try to run it via: cargo run --features unstable --example server --no-default-features

Using handlebars-iron?

Add your project to our adopters.

License

MIT, of course.



handlebars-iron

Handlebars 中间件 铁网框架

建立状态 的Gitter

这个图书馆,连同手柄,铁和超,工作 既稳定又有夜生锈。

铁杆和车把在0.x期间都有后向不兼容的变化 发布。所以你将需要选择手柄铁版本 您正在使用的两个版本:

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

用法

将HandlebarsEngine添加到您的Iron中间件链,作为之后 中间件。

  /// 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® = hbse.reload() { panic!("{}", r); }

chain.link_after(hbse);

如果要注册自己的自定义助手,可以初始化 HandlebarsEngine 从一个自定义的 Handlebars 注册表。

  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® = hbse.reload() { panic!("{}", r); }

chain.link_after(hbse);

您可以在 handlebars-rust中找到有关自定义帮助器的更多信息 文件

在您的处理程序中,将 Template 设置为响应。按照要求 Handlebars-rust,你的数据应该包含 serialize :: json :: ToJson 。如果 你在夜间频道,强烈推荐使用 tojson_macros 生成 默认 ToJson 实现而不重复你自己。

对于 DirectorySource ,句柄引擎将走路 由前缀指定,尝试注册所有与之匹配的模板 后缀,并将其名称提取为模板名称。例如, ./ examples / templates / some / path / index.hbs 将注册为 some / path / index

/// render data with "index" template
/// that is "./examples/templates/index.hbs"
fn helloworld(: &mut Request) -> IronResult<Response> {
    let mut resp = Response::new();

<span class="pl-k">let</span> data <span class="pl-k">=</span> ...
resp.<span class="pl-en">set_mut</span>(Template<span class="pl-k">::</span><span class="pl-en">new</span>(<span class="pl-s">&#34;index&#34;</span>, data)).<span class="pl-en">set_mut</span>(status<span class="pl-k">::</span><span class="pl-c1">Ok</span>);
<span class="pl-c1">Ok</span>(resp)

}

通过使用 Template :: with 您也可以渲染一些模板 实际注册。但是这不是推荐的,因为模板 每次都需要解析字符串。考虑使用 MemorySource 如果可能的话。

/// render data with "index" template
/// that is "./examples/templates/index.hbs"
fn helloworld(: &mut Request) -> IronResult<Response> {
    let mut resp = Response::new();

<span class="pl-k">let</span> data <span class="pl-k">=</span> ...
resp.<span class="pl-en">set_mut</span>(Template<span class="pl-k">::</span><span class="pl-en">with</span>(<span class="pl-s">&#34;&lt;h1&gt;{{title}}&lt;/h1&gt;&#34;</span>, data)).<span class="pl-en">set_mut</span>(status<span class="pl-k">::</span><span class="pl-c1">Ok</span>);
<span class="pl-c1">Ok</span>(resp)

}

由于这是简单的库,您可以运行这个 示例 RUST_LOG = handlebars_iron = info货运–example服务器 第一,和 文档 那么 Rust和它的生态系统还处于早期阶段 项目可能因为各种原因而被打破。我会尽力的 保持这个图书馆与1.0之前的最新的Rust编辑 最终发布。如果发现不好,请提出请求并发布报告 总是欢迎。

Live reload

在开发过程中,您可能希望在没有的情况下重新加载模板 必须重新启动您的网络服务器。这里是实时重新加载 功能。

因为实时重新加载只能在开发阶段有用,我们有 使其成为可选功能。为了实现它,你需要 在您的货物申报表中添加功能 watch

[features]
## create a feature in your app
watch = ["handlebars-iron/watch"]

[dependencies] handlebars-iron = ...

查看 examples / watch_server.rs 以获取更多信息。测试: RUST_LOG = handlebars_iron = info货运–example watch_server –features watch

Serde

从0.11.0,通过启用功能 serde_type ,您也可以使用 serde_json 作为类型 系统。通常你可以用你的类型来注释 #[derived(Serialize)] 使其成为模板数据。

您可以在 examples / server.rs 中找到一个工作示例,并尝试运行 它通过:货运–features unstable –example服务器–no-default-features

使用handlebars-铁?

将您的项目添加到我们的 采纳者

许可证

当然,麻省理工学院。




相关问题推荐