diff --git a/README.md b/README.md index 9ac47f3ba..3f491b737 100644 --- a/README.md +++ b/README.md @@ -92,35 +92,3 @@ See [comment_block.rs](https://github.com/cobalt-org/liquid-rust/blob/master/crates/lib/src/stdlib/blocks/comment_block.rs) for what a block implementation looks like. You can then register it by calling `liquid::ParserBuilder::block`. - -### Using partials - -To use `{% include %}` or `{% render %}` tags in your templates, you would first need to compile those -as template partials. - -Example: - -```liquid -# common.liquid -Number: {{ i }} -``` - -```rust -// Build template partials -type Partials = EagerCompiler; -let partials = { - let mut _partials = Partials::empty(); - - let filepath = String::from("common.liquid"); - let contents = std::fs::read_to_string(filepath).unwrap(); - - _partials.add(filepath, contents); - _partials -}; - -// Compile and render the main template -let parser = ParserBuilder::with_stdlib().partials(partials).build().unwrap(); -let rendered = parser.parse("Liquid! {% render "common", i: 42 %}").unwrap(); - -assert_eq!(rendered, "Liquid! Number: 42"); -``` diff --git a/crates/core/src/partials/mod.rs b/crates/core/src/partials/mod.rs index 3f0ecdd39..2228628f7 100644 --- a/crates/core/src/partials/mod.rs +++ b/crates/core/src/partials/mod.rs @@ -1,3 +1,36 @@ +//! # 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,no_run,compile_fail +//! // Build template partials using an eager, in-memory source compiler. +//! // Other compilation policies also exist depending on specific needs. +//! type Partials = EagerCompiler; +//! +//! let partials = { +//! let mut _partials = Partials::empty(); +//! +//! let filepath = String::from("common.liquid"); +//! let contents = std::fs::read_to_string(filepath).unwrap(); +//! +//! _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 = parser.parse("Liquid! {% render \"common\", i: 42 %}").unwrap(); +//! +//! assert_eq!(rendered, "Liquid! Number: 42"); +//! ``` + use std::borrow; use std::fmt; use std::sync;