Search code examples
google-app-enginejinja2webapp2

webapp2_extras.jinja2 compile template from variable


Having this property to get the instance of the class jinja2

@webapp2.cached_property
    def jinja2(self):
        # Returns an instance of :class:`Jinja2` from the app registry.
        return jinja2.get_jinja2(app=self.app)

Reference : http://webapp-improved.appspot.com/api/webapp2_extras/jinja2.html

I'm able to see that the returning class has the module Templates but I don't know how to import it.

I want to be able to load this module out of the returning class so I can do something like: temp = Template('{{name}}')

Help is appreciated!!!


Solution

  • This is what I did.

    values = { 'name' : 'user1791567' }
    
    import sys
    mod = sys.modules['jinja2.environment']
    
    _template = mod.Template('{{name}}')
    
    self.jinja2.render_template(_template, **values)
    

    And worked!!!..