jinja2.meta.find_undeclared_variables
In Jinja2 templating, managing variables is a crucial aspect of creating dynamic content. Among these variables, undeclared variables can cause issues if not properly handled. Jinja2's jinja2.meta.find_undeclared_variables
function is designed to help developers identify such variables within a template.
The find_undeclared_variables
function inspects the abstract syntax tree (AST) of a Jinja2 template and returns a set of undeclared variables. These are the variables that are referenced in the template but not declared within the context.
To use find_undeclared_variables
, you first need to import the required modules and parse your Jinja2 template:
from jinja2 import Environment, FileSystemLoader, meta
env = Environment(loader=FileSystemLoader('templates'))
template_source = env.loader.get_source(env, 'my_template.html')
parsed_content = env.parse(template_source)
Now you can call find_undeclared_variables
to identify the undeclared variables:
undeclared_variables = meta.find_undeclared_variables(parsed_content)
print(undeclared_variables)
This will output a set of undeclared variables in the 'my_template.html' file, such as:
{'variable1', 'variable2', 'variable3'}
Example Template Setter Class
Here's a Jinja template setter class that renders a template while checking for undeclared variables. If there are undeclared variables, it raises a ValueError
:
from dataclasses import dataclass, field
from jinja2 import Environment, meta, Template
@dataclass
class Jinja2TemplateSetter:
query: str
query_params: dict
template: Template = field(init=False)
parsed_content = field(init=False)
def __post_init__(self):
self.template = Template(self.query)
self.parsed_content = self._parse_template()
self._check_undeclared_variables()
def _parse_template(self):
return self.template.environment.parse(self.template.source)
def _check_undeclared_variables(self):
undeclared_variables = meta.find_undeclared_variables(self.parsed_content)
missing_variables = undeclared_variables - set(self.query_params.keys())
if missing_variables:
raise ValueError(f"Undeclared variables in the query: {', '.join(missing_variables)}")
def render_template(self):
return self.template.render(self.query_params)
# Usage
if __name__ == "__main__":
query = "{{ greeting }} {{ name }}, welcome to Jinja2!"
query_params = {"greeting": "Hello", "name": "Alice"}
try:
template_setter = Jinja2TemplateSetter(query=query, query_params=query_params)
print(template_setter.render_template())
except ValueError as e:
print(str(e))
Jinja2TemplateSetter
class initializes the Jinja2 Template object and parses the template content in the __post_init__
method. It then checks for undeclared variables using the _check_undeclared_variables
method. If there are any undeclared variables, a ValueError
will be raised. The render_template method is used to render the template using the provided query_params
.
References