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!` renders a registered template as a `HandlebarsResponse`.
46 A `200 OK` response includes `Content-Type`, `Content-Length`, and `ETag`.
47 A `304 Not Modified` response includes the `ETag` and has no body.
48 You can turn off HTML minification.
49* `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.
50* `handlebars_resources_initializer!` is used for generating a fairing for handlebars resources.
51
52## JSON Object Key Order
53
54`serde_json` sorts the keys of JSON objects in alphabetical order by default, so `{{#each}}` visits the keys of an object in that order.
55Enable the `preserve_json_order` feature to keep the insertion order instead.
56
57```toml
58[dependencies]
59rocket-include-handlebars = { version = "0.14", features = ["preserve_json_order"] }
60```
61
62This feature turns on the `preserve_order` feature of `serde_json`, which affects every crate in your dependency tree.
63*/
64
65#[macro_use]
66extern crate educe;
67
68#[doc(hidden)]
69pub extern crate manifest_dir_macros;
70
71mod functions;
72
73#[cfg(debug_assertions)]
74mod debug;
75
76#[cfg(not(debug_assertions))]
77mod release;
78
79mod macros;
80
81#[cfg(debug_assertions)]
82pub use debug::*;
83pub use handlebars::handlebars_helper;
84#[cfg(not(debug_assertions))]
85pub use release::*;
86pub use rocket_etag_if_none_match::{EtagIfNoneMatch, entity_tag::EntityTag};
87
88const DEFAULT_CACHE_CAPACITY: usize = 64;