j2cli icon indicating copy to clipboard operation
j2cli copied to clipboard

Enable Jinja2 whitespace control by default

Open m000 opened this issue 5 years ago • 0 comments

This patch turns on Jinja2 whitespace control by default. This will greatly reduce the need to use manual whitespace control in the templates in order to produce good looking output.

Rationale: Jinja2 on the web: The common use of Jinja2 is to serve web requests in realtime. The rendered templates are rarely inspected by a human and have a lifetime of a few minutes (at best). In this context, it is reasonable to turn off whitespace control to save some processing time and maximize throughput. Jinja2 on j2cli: j2cli is meant mostly as a batch processing tool. I.e. it is invoked only periodically. The generated output persists for much longer, and it is quite likely to be inspected by a human. Therefore, enabling whitespace control by default to produce better looking output makes sense.

Side-effects: There are barely any side-effects from this patch. Where manual space control is used in templates (e.g. {%- ... -%}) the - modifiers will become redundant. They can be eventually removed at a time of convenience. The only side-effect I've noticed is the case of consecutive {% include ... %} tags. In this case, whitespace control needs to be manually disabled using the + modifier. E.g. this:

{% include 'include/a.inc' %}
{% include 'include/b.inc' %}

becomes:

{%+ include 'include/a.inc' %}
{%+ include 'include/b.inc' %}

But I love whitespace in my output: In case someone absolutely hates this behaviour, it can be turned off with the new --no-compact flag.

m000 avatar Jul 28 '19 15:07 m000