How to get variables along with their filter name from JINJA2 template

Question:

I am working on a GAE (python) and JINJA based application. I have created a JINJA template from a text string using from_string method. i.e.

template = JINJA.from_string(text)

The result template looks like this:

Template(body=[Scope(body=[ScopedEvalContextModifier(options=[Keyword(key='autoescape', value=Name(name='on', ctx='load'))], body=[Output(nodes=[TemplateData(data=u'Dear '), Filter(node=Name(name='customer_name', ctx='load'), name='safe', args=[], kwargs=[], dyn_args=None, dyn_kwargs=None), TemplateData(data=u',nn'), Filter(node=Name(name='customer_name_new', ctx='load'), name='extra', args=[], kwargs=[], dyn_args=None, dyn_kwargs=None), TemplateData(data=u'n                    nThank you for choosing '), Name(name='company_name', ctx='load'), TemplateData(data=u'.nn')]), If(test=Name(name='start_datetime', ctx='load'), body=[Output(nodes=[TemplateData(data=u'Your '), Name(name='order_type', ctx='load'), TemplateData(data=u' is scheduled for:n'), Filter(node=Name(name='start_datetime_block', ctx='load'), name='safe', args=[], kwargs=[], dyn_args=None, dyn_kwargs=None), TemplateData(data=u'nYou can check out the estimated time of arrival for your '), Name(name='order_type', ctx='load'), TemplateData(data=u' using the button belown'), Filter(node=Name(name='live_link_button', ctx='load'), name='safe', args=[], kwargs=[], dyn_args=None, dyn_kwargs=None), TemplateData(data=u'n')])], else_=[Output(nodes=[TemplateData(data=u'Your '), Name(name='order_type', ctx='load'), TemplateData(data=u' is now placed.n')])]), If(test=And(left=Name(name='start_datetime', ctx='load'), right=Name(name='confirmation_required', ctx='load')), body=[Output(nodes=[TemplateData(data=u'Please confirm your availability for this appointment:n'), Filter(node=Name(name='confirmation_buttons', ctx='load'), name='safe', args=[], kwargs=[], dyn_args=None, dyn_kwargs=None), TemplateData(data=u'n')])], else_=[]), If(test=Name(name='custom_text', ctx='load'), body=[Output(nodes=[Filter(node=Name(name='custom_text', ctx='load'), name='safe', args=[], kwargs=[], dyn_args=None, dyn_kwargs=None), TemplateData(data=u'n')])], else_=[]), Output(nodes=[TemplateData(data=u'We look forward to seeing you. In case you have any questions please reach us at '), Name(name='company_email', ctx='load'), TemplateData(data=u'. '), Name(name='company_name', ctx='load'), TemplateData(data=u' '), Name(name='company_address', ctx='load'), TemplateData(data=u' '), Name(name='company_phone', ctx='load')])])])])

Now, what I want to do is that I want to get all the variables from this template and especially I am concerned with such variables which have some filters like in above template expected filters are safe and extra. Please note that extra is my custom filter.

Calling meta.find_undeclared_variables method only gives me list of keywords but, not their filters. i.e.

parsed_content = JINJA.parse(text)
keywords = meta.find_undeclared_variables(parsed_content)

Is there any way I can get keywords along with filter names as well?

Asked By: Hamid Raza

||

Answers:

Here is a simple solution that might help. It gives variable name with filters (only variables that have filters):

from jinja2 import Environment, PackageLoader, meta, nodes

def find_filters(ast):
    """Find all the nodes of a given type.  If the type is a tuple,
    the check is performed for any of the tuple items.
    """
    for child in ast.iter_child_nodes():
        if isinstance(child, nodes.Filter):
            yield child
        else:
          for result in find_filters(child):
              yield result


def filtered_variables(ast):
  """Return variables that have filters, along with their filters. might
  return duplicate variable names with different filters
  """
  results = []
  for i, node in enumerate(find_filters(ast)):
      filters = []
      f = node
      filters.append(f.name)
      while isinstance(f.node, nodes.Filter):
        f = f.node
        filters.append(f.name)
      filters.reverse()
      results.append((f.node.name, filters))
  return results


env = Environment(loader=PackageLoader('templates'))

template = '{% extends "layout.html" %}'
           '{% from "test.html" import a, b as c %}{{ some_variable | a | x}} {{ some_other }}'
           '{% import "meh.html" as meh %}{{ some_variable | b | c | d}}'
           '{% include "muh.html" %}'

ast = env.parse(template)
print(filtered_variables(ast))

The output will be:

[('some_variable', ['a', 'x']), ('some_variable', ['b', 'c', 'd'])]

You can include variables that has no filter like this:

f_vars = filtered_variables(ast)
filtered = []
for var in f_vars:
  filtered.append(var[0])
f = [(x, []) for x in keywords if x not in filtered]
f_vars.extend(f)
print(f_vars)

Output:

[('some_variable', ['a', 'x']), ('some_variable', ['b', 'c', 'd']), ('some_other', [])]

Note that result might have duplicate values. This can be more useful as different filters might be present on each occurrence of variable.

Answered By: maanijou
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.