Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a hint about deserializing helper params #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,33 @@
//! Data available to helper can be found in [Helper](struct.Helper.html). And there are more
//! examples in [HelperDef](trait.HelperDef.html) page.
//!
//! For complex helper implementations, the type of a parameter is more
//! convenient than its equivalent JSON representation. This requires those
//! types to implement `serde::Deserialize` in addition to `serde::Serialize`.
//! Be aware that the deserialization takes place on every call of the helper
//! and may be expensive!
//!
//! ```
//!# extern crate handlebars;
//!# #[macro_use]
//!# extern crate serde_derive;
//!# extern crate serde_json;
//!# use handlebars::{Handlebars, RenderContext, Helper, HelperResult, RenderError};
//!# fn main() {}
//!
//! #[derive(Deserialize)]
//! struct Foo;
//!
//! fn deserializing_helper(h: &Helper, _: &Handlebars, _: &mut RenderContext) -> HelperResult {
//! let a: Foo = h.param(0)
//! .ok_or(RenderError::new(format!("Parameter 0 is missing in {:?}", h)))
//! .map(|p| p.value().clone())
//! .and_then(|v| serde_json::from_value(v).map_err(|e| e.into()))?;
//! // do something with "a" of type Foo
//! Ok(())
//! }
//! ```
//!
//! You can learn more about helpers by looking into source code of built-in helpers.
//!
//! #### Built-in Helpers
Expand Down