|
13 | 13 | //! use handlebars::Handlebars;
|
14 | 14 | //!
|
15 | 15 | //! # fn main() {
|
16 |
| -//! // create the handlebars registry |
17 |
| -//! let mut handlebars = Handlebars::new(); |
18 |
| -//! |
19 |
| -//! // register the template. The template string will be verified and compiled. |
20 |
| -//! let source = "hello {{world}}"; |
21 |
| -//! assert!(handlebars.register_template_string("t1", source).is_ok()); |
22 |
| -//! |
23 |
| -//! // Prepare some data. |
24 |
| -//! // |
25 |
| -//! // The data type should implements `serde::Serialize` |
26 |
| -//! let mut data = BTreeMap::new(); |
27 |
| -//! data.insert("world".to_string(), "世界!".to_string()); |
28 |
| -//! assert_eq!(handlebars.render("t1", &data).unwrap(), "hello 世界!"); |
| 16 | +//! // create the handlebars registry |
| 17 | +//! let mut handlebars = Handlebars::new(); |
| 18 | +//! |
| 19 | +//! // register the template. The template string will be verified and compiled. |
| 20 | +//! let source = "hello {{world}}"; |
| 21 | +//! assert!(handlebars.register_template_string("t1", source).is_ok()); |
| 22 | +//! |
| 23 | +//! // Prepare some data. |
| 24 | +//! // |
| 25 | +//! // The data type should implements `serde::Serialize` |
| 26 | +//! let mut data = BTreeMap::new(); |
| 27 | +//! data.insert("world".to_string(), "世界!".to_string()); |
| 28 | +//! assert_eq!(handlebars.render("t1", &data).unwrap(), "hello 世界!"); |
29 | 29 | //! # }
|
30 | 30 | //! ```
|
31 | 31 | //!
|
|
126 | 126 | //! Templates are created from `String`s and registered to `Handlebars` with a name.
|
127 | 127 | //!
|
128 | 128 | //! ```
|
129 |
| -//! # extern crate handlebars; |
130 |
| -//! |
131 | 129 | //! use handlebars::Handlebars;
|
132 | 130 | //!
|
133 | 131 | //! # fn main() {
|
134 |
| -//! let mut handlebars = Handlebars::new(); |
135 |
| -//! let source = "hello {{world}}"; |
| 132 | +//! let mut handlebars = Handlebars::new(); |
| 133 | +//! let source = "hello {{world}}"; |
136 | 134 | //!
|
137 |
| -//! assert!(handlebars.register_template_string("t1", source).is_ok()) |
| 135 | +//! assert!(handlebars.register_template_string("t1", source).is_ok()) |
138 | 136 | //! # }
|
139 | 137 | //! ```
|
140 | 138 | //!
|
|
147 | 145 | //! without registration.
|
148 | 146 | //!
|
149 | 147 | //! ```
|
150 |
| -//! # use std::error::Error; |
151 | 148 | //! use handlebars::Handlebars;
|
152 | 149 | //! use std::collections::BTreeMap;
|
153 | 150 | //!
|
154 |
| -//! # fn main() -> Result<(), Box<dyn Error>> { |
155 |
| -//! let mut handlebars = Handlebars::new(); |
156 |
| -//! let source = "hello {{world}}"; |
| 151 | +//! # fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 152 | +//! let mut handlebars = Handlebars::new(); |
| 153 | +//! let source = "hello {{world}}"; |
157 | 154 | //!
|
158 |
| -//! let mut data = BTreeMap::new(); |
159 |
| -//! data.insert("world".to_string(), "世界!".to_string()); |
160 |
| -//! assert_eq!(handlebars.render_template(source, &data)?, "hello 世界!".to_owned()); |
| 155 | +//! let mut data = BTreeMap::new(); |
| 156 | +//! data.insert("world".to_string(), "世界!".to_string()); |
| 157 | +//! assert_eq!(handlebars.render_template(source, &data)?, "hello 世界!".to_owned()); |
161 | 158 | //! # Ok(())
|
162 | 159 | //! # }
|
163 | 160 | //! ```
|
|
179 | 176 | //! You can use default `render` function to render a template into `String`. From 0.9, there's `render_to_write` to render text into anything of `std::io::Write`.
|
180 | 177 | //!
|
181 | 178 | //! ```
|
182 |
| -//! # use std::error::Error; |
183 |
| -//! # #[macro_use] |
184 |
| -//! # extern crate serde_derive; |
185 |
| -//! # extern crate handlebars; |
186 |
| -//! |
187 | 179 | //! use handlebars::Handlebars;
|
188 | 180 | //!
|
189 |
| -//! #[derive(Serialize)] |
| 181 | +//! #[derive(serde::Serialize)] |
190 | 182 | //! struct Person {
|
191 | 183 | //! name: String,
|
192 | 184 | //! age: i16,
|
193 | 185 | //! }
|
194 | 186 | //!
|
195 |
| -//! # fn main() -> Result<(), Box<dyn Error>> { |
196 |
| -//! let source = "Hello, {{name}}"; |
197 |
| -//! |
198 |
| -//! let mut handlebars = Handlebars::new(); |
199 |
| -//! assert!(handlebars.register_template_string("hello", source).is_ok()); |
| 187 | +//! # fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 188 | +//! let source = "Hello, {{name}}"; |
200 | 189 | //!
|
| 190 | +//! let mut handlebars = Handlebars::new(); |
| 191 | +//! assert!(handlebars.register_template_string("hello", source).is_ok()); |
201 | 192 | //!
|
202 |
| -//! let data = Person { |
203 |
| -//! name: "Ning Sun".to_string(), |
204 |
| -//! age: 27 |
205 |
| -//! }; |
206 |
| -//! assert_eq!(handlebars.render("hello", &data)?, "Hello, Ning Sun".to_owned()); |
| 193 | +//! let data = Person { |
| 194 | +//! name: "Ning Sun".to_string(), |
| 195 | +//! age: 27 |
| 196 | +//! }; |
| 197 | +//! assert_eq!(handlebars.render("hello", &data)?, "Hello, Ning Sun".to_owned()); |
207 | 198 | //! # Ok(())
|
208 | 199 | //! # }
|
209 |
| -//! # |
210 | 200 | //! ```
|
211 | 201 | //!
|
212 | 202 | //! Or if you don't need the template to be cached or referenced by other ones, you can
|
213 | 203 | //! simply render it without registering.
|
214 | 204 | //!
|
215 | 205 | //! ```
|
216 |
| -//! # use std::error::Error; |
217 |
| -//! # #[macro_use] |
218 |
| -//! # extern crate serde_derive; |
219 |
| -//! # extern crate handlebars; |
220 | 206 | //! use handlebars::Handlebars;
|
221 |
| -//! # #[derive(Serialize)] |
| 207 | +//! # #[derive(serde::Serialize)] |
222 | 208 | //! # struct Person {
|
223 | 209 | //! # name: String,
|
224 | 210 | //! # age: i16,
|
225 | 211 | //! # }
|
226 | 212 | //!
|
227 |
| -//! # fn main() -> Result<(), Box<dyn Error>> { |
228 |
| -//! let source = "Hello, {{name}}"; |
| 213 | +//! # fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 214 | +//! let source = "Hello, {{name}}"; |
229 | 215 | //!
|
230 |
| -//! let mut handlebars = Handlebars::new(); |
| 216 | +//! let mut handlebars = Handlebars::new(); |
231 | 217 | //!
|
232 |
| -//! let data = Person { |
233 |
| -//! name: "Ning Sun".to_string(), |
234 |
| -//! age: 27 |
235 |
| -//! }; |
236 |
| -//! assert_eq!(handlebars.render_template("Hello, {{name}}", &data)?, |
237 |
| -//! "Hello, Ning Sun".to_owned()); |
| 218 | +//! let data = Person { |
| 219 | +//! name: "Ning Sun".to_string(), |
| 220 | +//! age: 27 |
| 221 | +//! }; |
| 222 | +//! assert_eq!( |
| 223 | +//! handlebars.render_template("Hello, {{name}}", &data)?, |
| 224 | +//! "Hello, Ning Sun".to_owned() |
| 225 | +//! ); |
238 | 226 | //! # Ok(())
|
239 | 227 | //! # }
|
240 | 228 | //! ```
|
241 | 229 | //!
|
242 | 230 | //! #### Escaping
|
243 | 231 | //!
|
244 |
| -//! As per the handlebars spec, output using `{{expression}}` is escaped by default (to be precise, the characters `&"<>'`=_` are replaced by their respective html / xml entities). However, since the use cases of a rust template engine are probably a bit more diverse than those of a JavaScript one, this implementation allows the user to supply a custom escape function to be used instead. For more information see the `EscapeFn` type and `Handlebars::register_escape_fn()` method. In particular, `no_escape()` can be used as the escape function if no escaping at all should be performed. |
| 232 | +//! As per the handlebars spec, output using `{{expression}}` is escaped by default (to be precise, the characters ``&"<>'`=_`` are replaced by their respective html / xml entities). However, since the use cases of a rust template engine are probably a bit more diverse than those of a JavaScript one, this implementation allows the user to supply a custom escape function to be used instead. For more information see the `EscapeFn` type and `Handlebars::register_escape_fn()` method. In particular, `no_escape()` can be used as the escape function if no escaping at all should be performed. |
245 | 233 | //!
|
246 | 234 | //! ### Custom Helper
|
247 | 235 | //!
|
248 | 236 | //! Handlebars is nothing without helpers. You can also create your own helpers with rust. Helpers in handlebars-rust are custom struct implements the `HelperDef` trait, concretely, the `call` function. For your convenience, most of stateless helpers can be implemented as bare functions.
|
249 | 237 | //!
|
250 | 238 | //! ```
|
251 | 239 | //! use std::io::Write;
|
252 |
| -//! # use std::error::Error; |
253 | 240 | //! use handlebars::*;
|
254 | 241 | //!
|
255 | 242 | //! // implement by a structure impls HelperDef
|
|
276 | 263 | //! }
|
277 | 264 | //!
|
278 | 265 | //!
|
279 |
| -//! # fn main() -> Result<(), Box<dyn Error>> { |
280 |
| -//! let mut handlebars = Handlebars::new(); |
281 |
| -//! handlebars.register_helper("simple-helper", Box::new(SimpleHelper)); |
282 |
| -//! handlebars.register_helper("another-simple-helper", Box::new(another_simple_helper)); |
283 |
| -//! // via closure |
284 |
| -//! handlebars.register_helper("closure-helper", |
285 |
| -//! Box::new(|h: &Helper, r: &Handlebars, _: &Context, rc: &mut RenderContext, out: &mut dyn Output| -> HelperResult { |
286 |
| -//! let param = |
287 |
| -//! h.param(0).ok_or(RenderErrorReason::ParamNotFoundForIndex("closure-helper", 0))?; |
288 |
| -//! |
289 |
| -//! out.write("3rd helper: ")?; |
290 |
| -//! out.write(param.value().render().as_ref())?; |
291 |
| -//! Ok(()) |
292 |
| -//! })); |
293 |
| -//! |
294 |
| -//! let tpl = "{{simple-helper 1}}\n{{another-simple-helper 2}}\n{{closure-helper 3}}"; |
295 |
| -//! assert_eq!(handlebars.render_template(tpl, &())?, |
296 |
| -//! "1st helper: 1\n2nd helper: 2\n3rd helper: 3".to_owned()); |
| 266 | +//! # fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 267 | +//! let mut handlebars = Handlebars::new(); |
| 268 | +//! handlebars.register_helper("simple-helper", Box::new(SimpleHelper)); |
| 269 | +//! handlebars.register_helper("another-simple-helper", Box::new(another_simple_helper)); |
| 270 | +//! // via closure |
| 271 | +//! handlebars.register_helper("closure-helper", |
| 272 | +//! Box::new(|h: &Helper, r: &Handlebars, _: &Context, rc: &mut RenderContext, out: &mut dyn Output| -> HelperResult { |
| 273 | +//! let param = |
| 274 | +//! h.param(0).ok_or(RenderErrorReason::ParamNotFoundForIndex("closure-helper", 0))?; |
| 275 | +//! |
| 276 | +//! out.write("3rd helper: ")?; |
| 277 | +//! out.write(param.value().render().as_ref())?; |
| 278 | +//! Ok(()) |
| 279 | +//! })); |
| 280 | +//! |
| 281 | +//! let tpl = "{{simple-helper 1}}\n{{another-simple-helper 2}}\n{{closure-helper 3}}"; |
| 282 | +//! assert_eq!( |
| 283 | +//! handlebars.render_template(tpl, &())?, |
| 284 | +//! "1st helper: 1\n2nd helper: 2\n3rd helper: 3".to_owned() |
| 285 | +//! ); |
297 | 286 | //! # Ok(())
|
298 | 287 | //! # }
|
299 |
| -//! |
300 | 288 | //! ```
|
301 | 289 | //!
|
302 | 290 | //! Data available to helper can be found in [Helper](struct.Helper.html). And there are more
|
|
371 | 359 | //!
|
372 | 360 | //! ```
|
373 | 361 | //! # #[cfg(feature = "string_helpers")] {
|
374 |
| -//! # use std::error::Error; |
375 |
| -//! # extern crate handlebars; |
376 | 362 | //! use handlebars::Handlebars;
|
377 | 363 | //!
|
378 |
| -//! # fn main() -> Result<(), Box<dyn Error>> { |
379 |
| -//! |
380 |
| -//! let mut handlebars = Handlebars::new(); |
| 364 | +//! # fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 365 | +//! let mut handlebars = Handlebars::new(); |
381 | 366 | //!
|
382 |
| -//! let data = serde_json::json!({"value": "lower camel case"}); |
383 |
| -//! assert_eq!(handlebars.render_template("This is {{lowerCamelCase value}}", &data)?, |
384 |
| -//! "This is lowerCamelCase".to_owned()); |
| 367 | +//! let data = serde_json::json!({"value": "lower camel case"}); |
| 368 | +//! assert_eq!( |
| 369 | +//! handlebars.render_template("This is {{lowerCamelCase value}}", &data)?, |
| 370 | +//! "This is lowerCamelCase".to_owned() |
| 371 | +//! ); |
385 | 372 | //! # Ok(())
|
386 | 373 | //! # }
|
387 | 374 | //! # }
|
|
0 commit comments