Debian Codegraph @

README.md

raw symbols SCIP
1 handlebars-rust
2 ===============
3
4 [Handlebars templating language](https://handlebarsjs.com) implemented
5 in Rust and for Rust.
6
7 [![CI](https://github.com/sunng87/handlebars-rust/actions/workflows/main.yml/badge.svg)](https://github.com/sunng87/handlebars-rust/actions/workflows/main.yml)
8 [![Coverage Status](https://coveralls.io/repos/github/sunng87/handlebars-rust/badge.svg?branch=master)](https://coveralls.io/github/sunng87/handlebars-rust?branch=master)
9 [![](https://img.shields.io/crates/v/handlebars)](https://crates.io/crates/handlebars)
10 [![](https://img.shields.io/crates/d/handlebars.svg)](https://crates.io/crates/handlebars)
11 [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)
12 [![Docs](https://docs.rs/handlebars/badge.svg)](https://docs.rs/crate/handlebars/)
13 [![Donate](https://img.shields.io/badge/donate-liberapay-yellow.svg)](https://liberapay.com/Sunng/donate)
14
15 ## Getting Started
16
17 ### Quick Start
18
19 ```rust
20 use handlebars::Handlebars;
21 use serde_json::json;
22 use std::error::Error;
23
24 fn main() -> Result<(), Box<dyn Error>> {
25 let mut reg = Handlebars::new();
26 // render without register
27 println!(
28 "{}",
29 reg.render_template("Hello {{name}}", &json!({"name": "foo"}))?
30 );
31
32 // register template using given name
33 reg.register_template_string("tpl_1", "Good afternoon, {{name}}")?;
34 println!("{}", reg.render("tpl_1", &json!({"name": "foo"}))?);
35
36 Ok(())
37 }
38 ```
39
40 ### Code Example
41
42 If you are not familiar with [handlebars language
43 syntax](https://handlebarsjs.com), it is recommended to walk through
44 their introduction first.
45
46 Examples are provided in source tree to demo usage of various api.
47
48 * [quick](https://github.com/sunng87/handlebars-rust/blob/master/examples/quick.rs)
49 the very basic example of registry and render apis
50 * [render](https://github.com/sunng87/handlebars-rust/blob/master/examples/render.rs)
51 how to define custom helpers with function, trait impl or macro, and also how
52 to use custom helpers.
53 * [render_file](https://github.com/sunng87/handlebars-rust/blob/master/examples/render_file.rs)
54 similar to render, but render to file instead of string
55 * [helper_macro](https://github.com/sunng87/handlebars-rust/blob/master/examples/helper_macro.rs)
56 demos usage of `handlebars_helper!` to simplify helper development
57 * [partials](https://github.com/sunng87/handlebars-rust/blob/master/examples/partials.rs)
58 template inheritance with handlebars
59 * [decorator](https://github.com/sunng87/handlebars-rust/blob/master/examples/decorator.rs)
60 how to use decorator to change data or define custom helper
61 * [script](https://github.com/sunng87/handlebars-rust/blob/master/examples/script.rs)
62 how to define custom helper with rhai scripting language,
63 just like using javascript for handlebarsjs
64 * [error](https://github.com/sunng87/handlebars-rust/blob/master/examples/error.rs)
65 simple case for error
66 * [dev_mode](https://github.com/sunng87/handlebars-rust/blob/master/examples/dev_mode.rs)
67 a web server hosts handlebars in `dev_mode`, you can edit the template and see the change
68 without restarting your server.
69
70 ### Web Playground
71
72 We have github action to compile latest `master` branch into WebAssembly and
73 serve it on [github pages](https://sunng87.github.io/handlebars-rust/). You can
74 test and verify your template with both handlebars-rust and handlebarjs.
75
76 ## Minimum Rust Version Policy
77
78 Handlebars will track Rust nightly and stable channel. When dropping
79 support for previous stable versions, I will bump **patch** version
80 and clarify in CHANGELOG.
81
82 ## Docs
83
84 [Rust doc](https://docs.rs/crate/handlebars/).
85
86 ## Changelog
87
88 Changelog is available in the source tree named as `CHANGELOG.md`.
89
90 ## Contributor Guide
91
92 Any contribution to this library is welcomed. To get started into
93 development, I have several [Help
94 Wanted](https://github.com/sunng87/handlebars-rust/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22)
95 issues, with the difficulty level labeled. When running into any problem,
96 feel free to contact me on github.
97
98 I'm always looking for maintainers to work together on this library,
99 let me know (via email or anywhere in the issue tracker) if you
100 want to join.
101
102 ## Why (this) Handlebars?
103
104 Handlebars is a real-world templating system that you can use to build
105 your application without pain.
106
107 ### Features
108
109 #### Isolation of Rust and HTML
110
111 This library doesn't attempt to use some macro magic to allow you to
112 write your template within your rust code. I admit that it's fun to do
113 that but it doesn't fit real-world use cases.
114
115 #### Limited but essential control structures built-in
116
117 Only essential control directives `if` and `each` are built-in. This
118 prevents you from putting too much application logic into your template.
119
120 #### Extensible helper system
121
122 You can write your own helper with Rust! It can be a block helper or
123 inline helper. Put your logic into the helper and don't repeat
124 yourself.
125
126 A helper can be as a simple as a Rust function like:
127
128 ```rust
129 handlebars_helper!(hex: |v: i64| format!("0x{:x}", v));
130
131 /// register the helper
132 handlebars.register_helper("hex", Box::new(hex));
133 ```
134
135 And using it in your template:
136
137 ```handlebars
138 {{hex 16}}
139 ```
140
141 By default, handlebars-rust ships [additional helpers](https://github.com/sunng87/handlebars-rust/blob/master/src/helpers/helper_extras.rs#L6)
142 (compared with original js version)
143 that is useful when working with `if`.
144
145 With `script_helper` feature flag enabled, you can also create helpers
146 using [rhai](https://github.com/jonathandturner/rhai) script, just like JavaScript
147 for handlebars-js. This feature was in early stage. Its API was limited at the
148 moment, and can change in future.
149
150 #### Template inheritance
151
152 Every time I look into a templating system, I will investigate its
153 support for [template
154 inheritance](https://docs.djangoproject.com/en/3.2/ref/templates/language/#template-inheritance).
155
156 Template include is not sufficient for template reuse. In most cases
157 you will need a skeleton of page as parent (header, footer, etc.), and
158 embed your page into this parent.
159
160 You can find a real example of template inheritance in
161 `examples/partials.rs` and templates used by this file.
162
163 #### Auto-reload in dev mode
164
165 By turning on `dev_mode`, handlebars auto reloads any template and scripts that
166 loaded from files or directory. This can be handy for template development.
167
168 #### WebAssembly compatible
169
170 Handlebars 3.0 can be used in WebAssembly projects.
171
172 #### Fully scriptable
173
174 With [rhai](https://github.com/rhaiscript/rhai) script support, you
175 can implement your own helper with the scripting language. Together
176 with the template lanaguage itself, template development can be fully
177 scriptable without changing rust code.
178
179 ## Related Projects
180
181 ### Web frameworks
182
183 * Iron: [handlebars-iron](https://github.com/sunng87/handlebars-iron)
184 * Rocket: [rocket/contrib](https://api.rocket.rs/v0.4/rocket_contrib/templates/index.html)
185 * Warp: [handlebars
186 example](https://github.com/seanmonstar/warp/blob/master/examples/handlebars_template.rs)
187 * Tower-web: [Built-in](https://github.com/carllerche/tower-web)
188 * Actix: [handlebars
189 example](https://github.com/actix/examples/blob/master/templating/handlebars/src/main.rs)
190 * Tide: [tide-handlebars](https://github.com/No9/tide-handlebars)
191 * Axum: [axum-template](https://github.com/Altair-Bueno/axum-template)
192
193 ### Adopters
194
195 The
196 [adopters](https://github.com/sunng87/handlebars-rust/wiki/Adopters)
197 page lists projects that uses handlebars for part of their
198 functionalities.
199
200 ### Extensions
201
202 The
203 [extensions](https://github.com/sunng87/handlebars-rust/wiki/Extensions)
204 page has libraries that provide additional helpers, decorators and
205 outputs to handlebars-rust, and you can use in your own projects.
206
207 ## License
208
209 This library (handlebars-rust) is open sourced under the MIT License.