7

We are using jinja2 to create our html but, because of the many loops and other things we do in jinja to produce the html, the html 'looks' ugly....(note: this is just for aesthetics). Is there anything we can do to clean up the html? (Other than the obvious of cleaning up our jinja2 code, which would make our template somewhat unreadable to us staffers)

Something like beautiful soup's prettify?

(Yes, I realize this question is a pretty nit-picky question...the ocd in me says to clean it up).

for instance:

                              <table>

      <tbody>


                  <tr>
                    <td>

                     a column

                    </td>




                                <td>

                                    a value

                                </td>
                      </tr>
                     </tbody>
           </table>     

Pretty ugly, eeh?

kristen
  • 478
  • 2
  • 10
  • 34

3 Answers3

5

Add '-' to the tags:

{%- if 'this'=='this' -%}
    {{ blah }}
{%- endif -%}
user_78361084
  • 3,538
  • 22
  • 85
  • 147
1

It looks like someone out there created a library to do just what need. See this library which I found attached to this question (whom you should upvote).

Community
  • 1
  • 1
sarwar
  • 2,835
  • 1
  • 26
  • 30
1

You can also configure Jinja to replace tags with nothing (instead of an empty line) by setting trim_blocks and lstrip_blocks to True. For example, in a Flask app, you might write:

app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True

The documentation explains whitespace control further.

c-x-berger
  • 991
  • 12
  • 30
  • 1
    While this looks like it should work it doesn't. I have experimented and can set and change these config variables, but they seem to be ignored when the template renders. By the way I did it in the ```before_app_request``` code so used ```current_app.jinja_env.trim_blocks = True``` for example. – Mark Kortink Feb 16 '20 at 22:36