pallets / jinja

A very fast and expressive template engine.

Home Page:https://jinja.palletsprojects.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request : Send an error (or warning) if the variable input fails.

shogysd opened this issue · comments

The following Python code and template will produce the following output

python code

from jinja2 import Environment, FileSystemLoader

template = Environment(loader=FileSystemLoader(".")).get_template("template.txt")

vals_0 = {"val_0": "Hello", "val_1": "World"}
vals_1 = {"val_0": "Hello"}

print("vals_0 :", str(template.render(vals_0)))
print("vals_1 :", str(template.render(vals_1)))

template

{{val_0}}, {{val_1}}!!

output

vals_0 : Hello, World!!
vals_1 : Hello, !!

vals_1's val_1 is empty because the substitution of val_1 failed.
There is concern that this behavior may generate unintended strings (e.g., empty links in HTML code) under certain conditions.
Therefore, if all template variables cannot be substituted, please give the programmer a chance to handle the error.

commented

What you want is StrictUndefined.

It seems to be exactly the feature I was looking for.
Thank you for your help.