tera icon indicating copy to clipboard operation
tera copied to clipboard

Whitespace control for include not working

Open pickfire opened this issue 4 years ago • 3 comments

{%- include "a.html" -%}
<world>
<hello>

Produces

<hello>
<world>

Instead of

<hello><world>

The whitespace creates an issue in https://github.com/rust-lang/docs.rs/blob/cdc7a5367f4dcf6517e42378e3c9bd1f470bd947/templates/rustdoc/topbar.html#L6-L8 which creates an extra whitespace in docs.rs, although there are other methods to solve it, like using the html tags correctly but ideally the whitespace should be removed as well.

image

cc @jyn514

pickfire avatar Feb 28 '21 12:02 pickfire

I believe the {%- and -%} of include should be handled differently where it not only trims the outer whitespace but also the inner ones https://github.com/Keats/tera/blob/b41e801b0449c8f8348276d4e1fe460c2d1bef0a/src/parser/whitespace.rs#L59

Another way is to allow using | trim for includes which I don't think is possible now.

pickfire avatar Feb 28 '21 13:02 pickfire

// https://github.com/Keats/tera/issues/610
#[test]
fn can_remove_whitespace_include_outer() {
    let mut tera = Tera::default();
    tera.add_raw_templates(vec![
        ("a.html", "{%- include 'b.html' -%}\n<world>"),
        ("b.html", "<hello>"),
    ]).unwrap();

    assert_eq!(tera.render("a.html", &Context::new()).unwrap(), "<hello><world>");
}

seems to pass. Can you tweak the test above to match your example? Github munches newlines.

Keats avatar Mar 06 '21 21:03 Keats

@Keats I think the tests that you showed is incorrect (real world usage), shouldn't it have \n at the end? Files usually end with newlines right?

// https://github.com/Keats/tera/issues/610
#[test]
fn can_remove_whitespace_include_outer() {
    let mut tera = Tera::default();
    tera.add_raw_templates(vec![
        ("a.html", "{%- include 'b.html' -%}\n<world>\n"),
        ("b.html", "<hello>\n"),
    ]).unwrap();

    assert_eq!(tera.render("a.html", &Context::new()).unwrap(), "<hello><world>");
}

pickfire avatar Mar 07 '21 17:03 pickfire