Skip to content

Instantly share code, notes, and snippets.

@opethe1st
Created June 27, 2018 23:08
Show Gist options
  • Save opethe1st/2c3f303d79b731e0de880392d99a3960 to your computer and use it in GitHub Desktop.
Save opethe1st/2c3f303d79b731e0de880392d99a3960 to your computer and use it in GitHub Desktop.
Describes using the expression statement
from jinja2 import FileSystemLoader
from jinja2 import Environment
from jinja2.ext import ExprStmtExtension
env = Environment(lstrip_blocks=True, loader=FileSystemLoader(searchpath='templates'), extensions=[ExprStmtExtension])
class Thing:
def __init__(self, a, b):
self.a = a
self.b = b
class Object:
def __init__(self, name, things=None):
self.name = name
self.things = things
class ObjectSaver:
def __init__(self):
self._dict = {}
def save(self, name, thing):
self._dict[name] = thing
return '#'
def getDict(self):
return self._dict
obj = Object(name='Object', things=[Thing(a='abc', b='def'), Thing(a='def', b='ghi')])
objectSaver = ObjectSaver()
print(env.get_template('sample.j2').render(obj=obj, objectSaver=objectSaver))
print(objectSaver.getDict())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment