Skip to content

Commit

Permalink
Merge pull request #542 from santoso-wijaya/document-partials
Browse files Browse the repository at this point in the history
Add documentation snippet for using partials
  • Loading branch information
epage authored Jun 3, 2024
2 parents 3db426b + 0017ca9 commit de393de
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
mod parser;
mod template;

pub mod partials;
pub mod reflection;

pub use liquid_core::partials;
/// Liquid data model.
pub mod model {
pub use liquid_core::array;
Expand Down
44 changes: 44 additions & 0 deletions src/partials.rs
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::*;

0 comments on commit de393de

Please sign in to comment.