jinja2.exceptions.TemplateAssertionError: no filter named 'raw'

While executing the below simple code in python i am getting jinja2.exceptions.TemplateAssertionError always.

Looks like filter 'raw' is not defined.

I was expecting the output like {"dns":["8.8.8.8", "8.8.4.4"]}

Any help would be appreciated.

from jinja2 import Template
import json, sys
from jinja2 import Template

OBJ = {"steps":[{"elements":[{}]}]}
OBJ['steps'][0]['elements'][0].update({'dns': '8.8.8.8,8.8.8.4'})
op = "{\"dns\":[\"{{steps[0].elements[0].dns|join('","')|raw}}\"}}\"]}"
template = Template(json.dumps(op))
payload= template.render(steps=OBJ['steps'])
print(payload)
sys.exit(1)
Traceback (most recent call last):
  File "/home/rashtrapathy/jj.py", line 9, in <module>
    template = Template(json.dumps(service_template_output))
  File "/usr/local/lib/python3.5/dist-packages/jinja2/environment.py", line 1031, in __new__
    return env.from_string(source, template_class=cls)
  File "/usr/local/lib/python3.5/dist-packages/jinja2/environment.py", line 941, in from_string
    return cls.from_code(self, self.compile(source), globals, None)
  File "/usr/local/lib/python3.5/dist-packages/jinja2/environment.py", line 638, in compile
    self.handle_exception(source=source_hint)
  File "/usr/local/lib/python3.5/dist-packages/jinja2/environment.py", line 832, in handle_exception
    reraise(*rewrite_traceback_stack(source=source))
  File "/usr/local/lib/python3.5/dist-packages/jinja2/_compat.py", line 28, in reraise
    raise value.with_traceback(tb)
  File "<unknown>", line 1, in template
jinja2.exceptions.TemplateAssertionError: no filter named 'raw'

Upvotes: 0

Views: 4906

Answers (2)

Timothy C. Quinn
Timothy C. Quinn

Reputation: 4495

You will need to define the raw filter. Not sure why this is not defined by default.

Here is code that works with current versions of flask / Jinja:

import jinja2
jinja2.filters.FILTERS['raw'] = lambda s: s

Upvotes: 0

Nishant Patel
Nishant Patel

Reputation: 1406

You can improved your code, by making adjustment like:

  1. Split the string prior to passing it in template
    dns = OBJ['steps'][0]['elements'][0]['dns'].split(',')
    OBJ['steps'][0]['elements'][0]['dns'] = dns
    
  2. Now create your own custom filter raw using jinja2 Environment filter dict, since its not there by default
     env = Environment(loader=FileSystemLoader(searchpath="."),
                       trim_blocks=True, lstrip_blocks=True)
     def raw(s): return s
     env.filters["raw"] = raw
    
    

If you want to perform any other operation inside raw function you can modify it according to your need.

Here's the updated code:

import json
from jinja2 import Environment, FileSystemLoader

def raw(s):
    return s

OBJ = {"steps":[{"elements":[{}]}]}
OBJ['steps'][0]['elements'][0].update({'dns': '8.8.8.8,8.8.4.4'})
dns = OBJ['steps'][0]['elements'][0]['dns'].split(',')

OBJ['steps'][0]['elements'][0]['dns'] = dns
op = "{\"dns\":[\"{{steps[0].elements[0].dns|join('","')|raw}}\"}}\"]}"

env = Environment(loader=FileSystemLoader(searchpath="."), trim_blocks=True, lstrip_blocks=True)
env.filters["raw"] = raw

template = env.from_string(json.dumps(op))
payload = template.render(steps=OBJ['steps'])
print(json.loads(payload))

Upvotes: 1

Related Questions