wrouesnel / p2cli

pongo2 cli - like the j2cli package in python, but compiles to self-contained go executables
GNU General Public License v2.0
87 stars 20 forks source link

autoescape tag prohibits use of extends tag #16

Closed kljensen closed 3 years ago

kljensen commented 3 years ago

The way the autoescape tag is used wraps the whole template in an {% autoescape off %} when autoescaping is disabled.

https://github.com/wrouesnel/p2cli/blob/a6e63265d9c918d7ec83c442b131a1d8b00796c8/p2cli.go#L235

If the template has an extends tag, the following error is raised by pongo2: "The 'extends' tag can only defined on root level.". That is, the extends tag cannot be placed within an autoescape tag. So, the way this code is structured, one may not use template inheritance with the autoescape option set to false.

I'm not sure of the most ergonomic way to fix this. It could be that one's best option is to turn autoescaping "on" and then to turn it off within the template. E.g.

p2cli --template ./templateB.j2 -i data.yaml -f yaml --autoescape 

where templateB.j2 has content

{% extends "templateA.j2" %}
{% block foo %}
overload from templateB
{% endblock %}

And templateA.j2 has content

{% autoscape off %}
{% block foo %}
default content from templateA
{% endblock %}
{% autoescape %}

I found this behavior using p2cli to template SQL files using inheritance.