Skip to content

Commit

Permalink
Move "Using partials" doc as proper Rustdoc in partials module
Browse files Browse the repository at this point in the history
  • Loading branch information
santoso-wijaya committed May 10, 2024
1 parent 2249b86 commit 8ec8242
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 32 deletions.
32 changes: 0 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<InMemorySource>;
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");
```
33 changes: 33 additions & 0 deletions crates/core/src/partials/mod.rs
Original file line number Diff line number Diff line change
@@ -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
//! // 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();
//!
//! _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;
Expand Down

0 comments on commit 8ec8242

Please sign in to comment.