0

I have problem with this 12 whitespaces in front and end of everything I send with Jinja2 to HTML.

I tried almost every advice which I found on web and nothing changes.

routes.py

@app.route("/test", methods=["GET", "POST"])
def test():
    form = TestForm()
    if form.validate_on_submit():
        output = test_procesing(test_input=form.test_input.data)
        return render_template("test.html", form=form, output=output)
    return render_template("test.html", form=form)

test.py

def test_procesing(test_input):
    output = test_input
    return output

Template:

        <div class="col-lg-8">
            <pre>
            {{ output }}
            </pre>
        </div>

Result: visual result

Result in editor with whitespaces visible:

            123456789
            
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
stam mb
  • 11
  • Does this answer your question? [Remove unnecessary whitespace from Jinja rendered template](https://stackoverflow.com/questions/35775207/remove-unnecessary-whitespace-from-jinja-rendered-template) – β.εηοιτ.βε Feb 13 '22 at 14:05
  • @β.εηοιτ.βε That doesn't help here, since OP isn't using any Jinja blocks. – AKX Feb 13 '22 at 14:16
  • @AKX is it? `{{ output }}` is a Jinja expression bock, so it applies. – β.εηοιτ.βε Feb 13 '22 at 14:21
  • 1
    @β.εηοιτ.βε Sorry, yeah, you're actually correct, the answer just doesn't show you can also use `-` with expressions too. – AKX Feb 13 '22 at 14:40

1 Answers1

2

You're putting those twelve spaces there in your markup. (The <pre> tag is special in that it doesn't consume the inner whitespace in the tag.)

Simply: Instead of

            <pre>
            {{ output }}
            </pre>

do

            <pre>{{ output }}</pre>

or if you do want to keep the three-lined layout, you can add -s to have Jinja eat the whitespace:

            <pre>
            {{- output -}}
            </pre>
AKX
  • 152,115
  • 15
  • 115
  • 172