mattrobenolt / jinja2-cli

CLI for Jinja2
BSD 2-Clause "Simplified" License
506 stars 181 forks source link

Parsing input variable JSON on one line breaks #108

Closed BenAlanSouthall closed 1 year ago

BenAlanSouthall commented 1 year ago

MRPE:

$ cat sample.json |                                   jinja2 --format=json sample.html.jinja2
<html>
<ul>
    <li>foo</li>
    <li>bar</li>
    <li>baz</li>
    </ul>

</html>
$ cat sample.json | jq -c | tee sample.oneLine.json | jinja2 --format=json sample.html.jinja2
<html>
<ul>
    </ul>

</html>

IUsing the samples given, it seems placing all the iunput on one line messes with the input parsing, even when I try to force it. Variables are not defined, if I use the --strict flag.

mattrobenolt commented 1 year ago

👀

Very interesting and good reproduction steps. Lemme see if I can reproduce myself and if I can see anything obviously wrong.

mattrobenolt commented 1 year ago

This worked just fine for me locally?

[main][jinja2-cli/samples]$ cat sample.json | jq -c | tee sample.oneLine.json | jinja2 --format=json sample.jinja2
<html>

<ul>
    <li>foo</li>
    <li>bar</li>
    <li>baz</li>
    </ul>

</html>

[main][jinja2-cli/samples]$ cat sample.oneLine.json
{"items":["foo","bar","baz"]}
[main][jinja2-cli/samples]$ cat sample.json
{
    "items": [
        "foo",
        "bar",
        "baz"
    ]
}
[main][jinja2-cli/samples]$ cat sample.jinja2
<html>

{% if foo is defined -%}
<!-- foo was defined -->
{% endif -%}

{% if bar is defined -%}
<h1>Hey it's there!</h1>
{% endif -%}

<ul>
    {% for item in items -%}
    <li>{{ item }}</li>
    {% endfor -%}
</ul>

</html>
BenAlanSouthall commented 1 year ago

It's certainly strange. Could it be dependent packages that PIP hasn't bumped when installing the CLI? I.e. we are on different versions of some dependency. Or even due to different python versions.

In either case, I moved over to j2 instead and only need this for something small, so am happy if you want to close for now, and if it crops up again in the wild someone else can re-open

mattrobenolt commented 1 year ago

Yeah, not entirely sure, this was manually tested against python 3.10, and just installed from main. I've not heard of any issues like this before, since the formatting shouldn't matter at all.

Oh well, sorry it didn't work out for you.