rocket_include_handlebars/lib.rs
1/*!
2# Include Handlebars Templates for Rocket Framework
3
4This is a crate which provides macros `handlebars_resources_initialize!` and `handlebars_response!` to statically include HBS (Handlebars) files from your Rust project and make them be the HTTP response sources quickly.
5
6## Example
7
8```rust,ignore
9#[macro_use]
10extern crate rocket;
11
12#[macro_use]
13extern crate rocket_include_handlebars;
14
15use std::collections::HashMap;
16
17use rocket::State;
18
19use rocket_include_handlebars::{EtagIfNoneMatch, HandlebarsContextManager, HandlebarsResponse};
20
21#[get("/")]
22fn index(
23 handlebars_cm: &State<HandlebarsContextManager>,
24 etag_if_none_match: EtagIfNoneMatch,
25) -> HandlebarsResponse {
26 let mut map = HashMap::new();
27
28 map.insert("title", "Title");
29 map.insert("body", "Hello, world!");
30
31 handlebars_response!(handlebars_cm, etag_if_none_match, "index", map)
32}
33
34#[launch]
35fn rocket() -> _ {
36 rocket::build()
37 .attach(handlebars_resources_initializer!(
38 "index" => "views/index.hbs",
39 ))
40 .mount("/", routes![index])
41}
42```
43
44* `handlebars_resources_initialize!` is used in the fairing of `HandlebarsResponse` to include Handlebars files into your executable binary file. You need to specify each file's name and its path relative to the directory containing the manifest of your package. In order to reduce the compilation time and allow to hot-reload templates, files are compiled into your executable binary file together, only when you are using the **release** profile.
45* `handlebars_response!` is used for retrieving and rendering the file you input through the macro `handlebars_resources_initialize!` as a `HandlebarsResponse` instance with rendered HTML. When its `respond_to` method is called, three HTTP headers, **Content-Type**, **Content-Length** and **Etag**, will be automatically added, and the rendered HTML can optionally not be minified.
46* `handlebars_response_cache!` is used for wrapping a `HandlebarsResponse` and its constructor, and use a **key** to cache its HTML and ETag in memory. The cache is generated only when you are using the **release** profile.
47* `handlebars_resources_initializer!` is used for generating a fairing for handlebars resources.
48*/
49
50#[macro_use]
51extern crate educe;
52
53#[doc(hidden)]
54pub extern crate manifest_dir_macros;
55
56mod functions;
57
58#[cfg(debug_assertions)]
59mod debug;
60
61#[cfg(not(debug_assertions))]
62mod release;
63
64mod macros;
65
66#[cfg(debug_assertions)]
67pub use debug::*;
68pub use handlebars::handlebars_helper;
69#[cfg(not(debug_assertions))]
70pub use release::*;
71pub use rocket_etag_if_none_match::{EtagIfNoneMatch, entity_tag::EntityTag};
72
73const DEFAULT_CACHE_CAPACITY: usize = 64;