-1

Hi I need to send data in string format to my jinja templates, and the render them. The only way I found is to format my data as JSON and send it as a string to the renderer. But I don´t know how to use it in the templates, it seems that the tojson filter it´s not for this purpose, because it keeps rendered a string.

to keep it simple I'm doing something similar to:

import json

a = {"a":1,"b":[1,2,3,4]}
response = json.dumps(a)

in template:

{{ response }}
{{ response|tojson }}

both give a string response, not a dict or an object that I can use to render based on the values

efirvida
  • 4,592
  • 3
  • 42
  • 68

1 Answers1

1

You can import json to use it's load function to load it into jinja.

from json import loads

environment = jinja2.Environment(whatever)
environment.filters['load'] = loads
{{ response|load }}

Reference: Import a Python module into a Jinja template?

efirvida
  • 4,592
  • 3
  • 42
  • 68
walker
  • 444
  • 2
  • 12