This module provides Mako template support for webapp2.
To use it, you must add the mako
package to your application
directory (for App Engine) or install it in your virtual environment
(for other servers).
You can download mako
from PyPi:
Learn more about Mako:
webapp2_extras.mako.
default_config
= {'template_path': 'templates'}¶Default configuration values for this module. Keys are:
webapp2_extras.mako.
Mako
(app, config=None)[source]¶Wrapper for configurable and cached Mako environment.
To used it, set it as a cached property in a base RequestHandler:
import webapp2
from webapp2_extras import mako
class BaseHandler(webapp2.RequestHandler):
@webapp2.cached_property
def mako(self):
# Returns a Mako renderer cached in the app registry.
return mako.get_mako(app=self.app)
def render_response(self, _template, **context):
# Renders a template and writes the result to the response.
rv = self.mako.render_template(_template, **context)
self.response.write(rv)
Then extended handlers can render templates directly:
class MyHandler(BaseHandler):
def get(self):
context = {'message': 'Hello, world!'}
self.render_response('my_template.html', **context)
render_template
(_filename, **context)[source]¶Renders a template and returns a response object.
Parameters: |
|
---|---|
Returns: | A rendered template. |
webapp2_extras.mako.
get_mako
(factory=<class 'webapp2_extras.mako.Mako'>, key='webapp2_extras.mako.Mako', app=None)[source]¶Returns an instance of Mako
from the app registry.
It’ll try to get it from the current app registry, and if it is not registered it’ll be instantiated and registered. A second call to this function will return the same instance.
Parameters: |
|
---|
webapp2_extras.mako.
set_mako
(mako, key='webapp2_extras.mako.Mako', app=None)[source]¶Sets an instance of Mako
in the app registry.
Parameters: |
|
---|