mitsuhiko / minijinja

MiniJinja is a powerful but minimal dependency template engine for Rust compatible with Jinja/Jinja2
https://docs.rs/minijinja/
Apache License 2.0
1.64k stars 99 forks source link

set_lstrip_blocks should not apply to expressions, only to blocks. #496

Closed derrley closed 6 months ago

derrley commented 6 months ago

Description

I've been using the newly added support for set_lstrip_blocks. Thank you for that! I've noticed, though, that i think it is also stripping leading spaces from expressions -- {{ }}. Those aren't blocks, right? If an expression is positioned with leading whitespace in the file, it should stay that way, I think.

Here's the way it works in python:

[16:52:32][kylederr@KylesPenalMBPM3][~/Code/derrley/testing_jinja]
$ python3 script.py
    one
    two

(venv)
[16:52:33][kylederr@KylesPenalMBPM3][~/Code/derrley/testing_jinja]
$ cat script.py
import jinja2
templateLoader = jinja2.FileSystemLoader(searchpath=".")
templateEnv = jinja2.Environment(
  loader=templateLoader,
  lstrip_blocks=True,
  trim_blocks=True
)
template = templateEnv.get_template('tmpl.tmp')
templateVars = {
  "things": [
    "one",
    "two",
  ]
}

print(template.render(templateVars))
(venv)
[16:52:48][kylederr@KylesPenalMBPM3][~/Code/derrley/testing_jinja]
$ cat tmpl.tmp
{% for thing in things %}
    {{ thing }}
{% endfor %}

Reproduction steps

Do the above, in rust:

use minijinja::{context, path_loader, Environment};

fn main() {
    let mut env = Environment::new();
    env.set_trim_blocks(true);
    env.set_lstrip_blocks(true);
    env.set_loader(path_loader("."));
    let tmpl = env.get_template("tmpl.tmp").unwrap();
    println!(
        "{}",
        tmpl.render(context! {things => vec!["one", "two"]})
            .unwrap()
    )
}

and get this output:

$ cargo run --bin testtmpl
   Compiling rules v0.1.0 (/Users/kylederr/Code/perpetualsystems/simba/rules)
    Finished dev [unoptimized + debuginfo] target(s) in 0.17s
     Running `/Users/kylederr/Code/perpetualsystems/simba/target/debug/testtmpl`
one
two

(note they are not indented)

Additional helpful information:

What did you expect

For the blocks to be indented, as per above.