pallets / jinja

A very fast and expressive template engine.
https://jinja.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
10.23k stars 1.6k forks source link

DebugUndefined does not retain filters in variable #1888

Closed paimoe closed 11 months ago

paimoe commented 11 months ago

Description

When using DebugUndefined to make a partial template, the filters are not kept if a variable is not passed to the template. Similar-ish to #1871 and other DebugUndefined issues.

Replication

>>> import jinja2
>>> jinja2.__version__
'3.1.2'
>>> env = jinja2.Environment(undefined=jinja2.DebugUndefined)
>>> template = env.from_string("{{ var|safe }}")
>>> template.render()
'{{ var }}'
>>> template.render(var="hello")
'hello'
>>> 

Expectation

I'm not sure 100% sure if this is the best way to do a partial template rendering, but was all I found on Stackoverflow and other places.

https://stackoverflow.com/questions/12994028/multiple-renders-of-jinja2-templates (old)

Environment:

Workaround

My current workaround is just a string replace like

base_template = base_template.replace(
        "var", "var | safe"
    )
ThiefMaster commented 11 months ago

I'm pretty sure that your usecase is not what DebugUndefined is meant for - it's simply meant to be a way to quickly see the variable that's missing. Not to get something that's again to be used as a functional jinja template....