Keats / tera

A template engine for Rust based on Jinja2/Django
http://keats.github.io/tera/
MIT License
3.36k stars 280 forks source link

[Question] Help understanding the `indent` built-in #917

Open apiraino opened 2 months ago

apiraino commented 2 months ago

I am looking at the new indent built-in introduces recently (merged in #798).

At first I was looking into slightly clarifying the documentation wording, see docs, but then went down this rabbit hole and found myself confused on how to use it.

Example main.rs:

use tera::{Context, Tera}; // v1.19.1

fn main() {
    let context = Context::new();
    let tera = Tera::new("./*.tt").unwrap();
    let res = tera.render("test.tt", &context).unwrap();
    println!("{}", res);
}

Tera template test.tt:

// testing `prefix`
-{{ "TEXT" | indent(prefix="") }}
-{{ "TEXT" | indent(prefix="    ") }}

// testing `first`
-{{ "one\n\ntwo\nthree" | indent }}
-{{ "one\n\ntwo\nthree" | indent(first=true) }}

// testing `blank`
-{{ "   ABCD" | indent(blank=true) }}
-{{ "   ABCD" | indent(blank=false) }}
-{{ "   " | indent(blank=false) }}
-{{ "   " | indent(blank=true) }}

Will be rendered as:

// testing `prefix`
-TEXT
-TEXT

// testing `first`
-one\n\ntwo\nthree
-    one\n\ntwo\nthree

// testing `blank`
-   ABCD
-   ABCD
-   
-

Question for @untereiner (author of the #798 ): can you help me understand how the flags blank,first and prefix actually work? The above template rendering is slightly confusing for me.

Thanks!