Skip to content

Commit 84e85fc

Browse files
authored
Merge pull request #668 from mrnossiom/master
docs: fix code examples formatting
2 parents a4b978e + c10a028 commit 84e85fc

File tree

2 files changed

+80
-96
lines changed

2 files changed

+80
-96
lines changed

src/lib.rs

Lines changed: 72 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
//! use handlebars::Handlebars;
1414
//!
1515
//! # 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 世界!");
2929
//! # }
3030
//! ```
3131
//!
@@ -126,15 +126,13 @@
126126
//! Templates are created from `String`s and registered to `Handlebars` with a name.
127127
//!
128128
//! ```
129-
//! # extern crate handlebars;
130-
//!
131129
//! use handlebars::Handlebars;
132130
//!
133131
//! # fn main() {
134-
//! let mut handlebars = Handlebars::new();
135-
//! let source = "hello {{world}}";
132+
//! let mut handlebars = Handlebars::new();
133+
//! let source = "hello {{world}}";
136134
//!
137-
//! assert!(handlebars.register_template_string("t1", source).is_ok())
135+
//! assert!(handlebars.register_template_string("t1", source).is_ok())
138136
//! # }
139137
//! ```
140138
//!
@@ -147,17 +145,16 @@
147145
//! without registration.
148146
//!
149147
//! ```
150-
//! # use std::error::Error;
151148
//! use handlebars::Handlebars;
152149
//! use std::collections::BTreeMap;
153150
//!
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}}";
157154
//!
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());
161158
//! # Ok(())
162159
//! # }
163160
//! ```
@@ -179,77 +176,67 @@
179176
//! 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`.
180177
//!
181178
//! ```
182-
//! # use std::error::Error;
183-
//! # #[macro_use]
184-
//! # extern crate serde_derive;
185-
//! # extern crate handlebars;
186-
//!
187179
//! use handlebars::Handlebars;
188180
//!
189-
//! #[derive(Serialize)]
181+
//! #[derive(serde::Serialize)]
190182
//! struct Person {
191183
//! name: String,
192184
//! age: i16,
193185
//! }
194186
//!
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}}";
200189
//!
190+
//! let mut handlebars = Handlebars::new();
191+
//! assert!(handlebars.register_template_string("hello", source).is_ok());
201192
//!
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());
207198
//! # Ok(())
208199
//! # }
209-
//! #
210200
//! ```
211201
//!
212202
//! Or if you don't need the template to be cached or referenced by other ones, you can
213203
//! simply render it without registering.
214204
//!
215205
//! ```
216-
//! # use std::error::Error;
217-
//! # #[macro_use]
218-
//! # extern crate serde_derive;
219-
//! # extern crate handlebars;
220206
//! use handlebars::Handlebars;
221-
//! # #[derive(Serialize)]
207+
//! # #[derive(serde::Serialize)]
222208
//! # struct Person {
223209
//! # name: String,
224210
//! # age: i16,
225211
//! # }
226212
//!
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}}";
229215
//!
230-
//! let mut handlebars = Handlebars::new();
216+
//! let mut handlebars = Handlebars::new();
231217
//!
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+
//! );
238226
//! # Ok(())
239227
//! # }
240228
//! ```
241229
//!
242230
//! #### Escaping
243231
//!
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.
245233
//!
246234
//! ### Custom Helper
247235
//!
248236
//! 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.
249237
//!
250238
//! ```
251239
//! use std::io::Write;
252-
//! # use std::error::Error;
253240
//! use handlebars::*;
254241
//!
255242
//! // implement by a structure impls HelperDef
@@ -276,27 +263,28 @@
276263
//! }
277264
//!
278265
//!
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+
//! );
297286
//! # Ok(())
298287
//! # }
299-
//!
300288
//! ```
301289
//!
302290
//! Data available to helper can be found in [Helper](struct.Helper.html). And there are more
@@ -371,17 +359,16 @@
371359
//!
372360
//! ```
373361
//! # #[cfg(feature = "string_helpers")] {
374-
//! # use std::error::Error;
375-
//! # extern crate handlebars;
376362
//! use handlebars::Handlebars;
377363
//!
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();
381366
//!
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+
//! );
385372
//! # Ok(())
386373
//! # }
387374
//! # }

src/macros.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,24 @@
1313
/// # Examples
1414
///
1515
/// ```rust
16-
/// #[macro_use] extern crate handlebars;
17-
/// #[macro_use] extern crate serde_json;
18-
///
16+
/// # use handlebars::{handlebars_helper, Handlebars};
17+
/// # use serde_json::json;
1918
/// handlebars_helper!(is_above_10: |x: u64| x > 10);
2019
/// handlebars_helper!(is_above: |x: u64, { compare: u64 = 10 }| x > compare);
2120
///
22-
/// # fn main() {
23-
/// #
24-
/// let mut handlebars = handlebars::Handlebars::new();
21+
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
22+
/// let mut handlebars = Handlebars::new();
2523
/// handlebars.register_helper("is-above-10", Box::new(is_above_10));
2624
/// handlebars.register_helper("is-above", Box::new(is_above));
2725
///
2826
/// let result = handlebars
29-
/// .render_template("{{#if (is-above-10 12)}}great!{{else}}okay{{/if}}", &json!({}))
30-
/// .unwrap();
27+
/// .render_template("{{#if (is-above-10 12)}}great!{{else}}okay{{/if}}", &json!({}))?;
3128
/// assert_eq!(&result, "great!");
29+
///
3230
/// let result2 = handlebars
33-
/// .render_template("{{#if (is-above 12 compare=10)}}great!{{else}}okay{{/if}}", &json!({}))
34-
/// .unwrap();
31+
/// .render_template("{{#if (is-above 12 compare=10)}}great!{{else}}okay{{/if}}", &json!({}))?;
3532
/// assert_eq!(&result2, "great!");
36-
/// # }
33+
/// # Ok(()) }
3734
/// ```
3835
3936
#[macro_export]

0 commit comments

Comments
 (0)