Skip to main content

handlebars_iron/
lib.rs

1//! # Handlebars for Iron
2//!
3//! This library combines [Handlebars templating library](https://github.com/sunng87/handlebars-rust) and [Iron web framework](http://ironframework.io) together. It gives you a `HandlebarsEngine` as Iron `AfterMiddleware`, so you can render your data with specified handlebars template.
4//!
5//! ## Setup
6//!
7//! Handlebars-iron provides two kinds of template source: `DirectorySource` and `MemorySource` by default. `DirectorySource` helps you to load template files from a directory, which `MemorySource` loads template as string in memory.
8//!
9//! To load files from file system, you need to specify template root and file suffix. Handlebars-iron will scan the directory and loads all templates that matches the suffix. The file's relative path name will be applied as template name.
10//!
11//! ```ignore
12//! /// HandlebarsEngine will look up all files with "./examples/templates/**/*.hbs"
13//! let mut hbse = HandlebarsEngine::new();
14//! hbse.add(Box::new(DirectorySource::new("./examples/templates/", ".hbs")));
15//!
16//! // load templates from all registered sources
17//! if let Err(r) = hbse.reload() {
18//!   panic!("{}", r.description());
19//! }
20//!
21//! chain.link_after(hbse);
22//!
23//! ```
24//!
25//! ## Usage
26//!
27//! From any of your handler, you can set template name and data into our `Template` struct. Remember you need to make your data implements `serde::Serialize`, which is required by handlebars-rust.
28//!
29//! We have implemented Modifier for `Template` on `Response`, so you can just use `response.set` to put set template into response and let it processed by our middleware.
30//!
31//! Also we made `Response` plugin for `Template` via `HandlebarsEngine`. So you can test your handler from a test case, and retrieve the `Template` you set into it by `response.get::<HandlebarsEngine>`.
32//!
33
34pub extern crate handlebars;
35
36extern crate iron;
37
38extern crate serde;
39extern crate serde_json;
40
41#[cfg(feature = "watch")]
42extern crate notify;
43extern crate plugin;
44
45#[macro_use]
46extern crate log;
47
48pub use self::middleware::HandlebarsEngine;
49pub use self::middleware::Template;
50pub use self::source::{Source, SourceError};
51pub use self::sources::directory::DirectorySource;
52pub use self::sources::memory::MemorySource;
53#[cfg(feature = "watch")]
54pub use self::watch::Watchable;
55
56mod middleware;
57mod source;
58mod sources;
59#[cfg(feature = "watch")]
60mod watch;