noahmorrison / chevron

A Python implementation of mustache
MIT License
505 stars 55 forks source link

Support error raising on missing variables and partials #109

Open tzoiker opened 2 years ago

tzoiker commented 2 years ago

Due to security issues we decided to migrate from jinja to mustache. Jinja supports error raising for missing variables, which is good for data validation purposes. Proposition:

lekhnath commented 1 year ago

When will this get merged?

Cayan commented 1 year ago

I'm using the following logic to validate if there are missing variables:

rendered_content = chevron.render(template, context)

# Find all variables in the template
variables = re.findall(r'{{\s*([\w.]+)\s*}}', template)

# Check if any variable resulted in an empty string
for var in variables:
    var_rendered = chevron.render("{{" + var + "}}", context)
    if var_rendered == "":
        raise Exception(f"Variable '{var}' is missing from context or resulted in an empty string.")
seittema commented 6 months ago

@noahmorrison Any indication if this can/will get merged? We are in need of this behavior as well to be able to catch an exception when a there is a missing variables as that indicates there's a bug and we do not want to silently continue