-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #542 from santoso-wijaya/document-partials
Add documentation snippet for using partials
- Loading branch information
Showing
2 changed files
with
45 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//! ## Using partials | ||
//! | ||
//! To use `{% include %}` or `{% render %}` tags in a template, you first need to compile these | ||
//! included files as template partials. | ||
//! | ||
//! Example: | ||
//! | ||
//! ```liquid | ||
//! # common.liquid | ||
//! Number: {{ i }} | ||
//! ``` | ||
//! | ||
//! ```rust | ||
//! use liquid::ParserBuilder; | ||
//! use liquid::partials::{EagerCompiler, InMemorySource, PartialSource}; | ||
//! | ||
//! // Build template partials using an eager, in-memory source compiler. | ||
//! // Other compilation policies also exist depending on specific needs. | ||
//! type Partials = EagerCompiler<InMemorySource>; | ||
//! | ||
//! let partials = { | ||
//! let mut partials = Partials::empty(); | ||
//! | ||
//! let filepath = String::from("common.liquid"); | ||
//! //let contents = std::fs::read_to_string(&filepath).unwrap(); | ||
//! let contents = "Number: {{ i }}"; | ||
//! | ||
//! partials.add(filepath, contents); | ||
//! partials | ||
//! }; | ||
//! | ||
//! // Compile and render the main template, which uses the "common" partial. | ||
//! let parser = ParserBuilder::with_stdlib().partials(partials).build().unwrap(); | ||
//! let rendered = { | ||
//! let mut globals = liquid::object!({ "num": 42 }); | ||
//! parser | ||
//! .parse("Liquid! {% render \"common\", i: num %}").unwrap() | ||
//! .render(&globals).unwrap() | ||
//! }; | ||
//! | ||
//! assert_eq!(rendered, "Liquid! Number: 42"); | ||
//! ``` | ||
pub use liquid_core::partials::*; |