Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support error raising on missing variables and partials #109

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tzoiker
Copy link

@tzoiker tzoiker commented Nov 17, 2021

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:

  • Add error flag to render (incompatible with keep, warn);
  • Raise UndefinedError only for variable, no escape, partial.

@lekhnath
Copy link

When will this get merged?

@Cayan
Copy link

Cayan commented Aug 23, 2023

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
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants