Mako Template Bindings for Pyramid
Pyramid_mako provides a set of bindings that integrate the Mako templating system with the Pyramid web framework. It enables Pyramid applications to render dynamic content using Mako templates, which compile into Python modules for optimal performance. The library is currently at version 1.1.0 and is actively maintained by the Pylons Project, receiving updates to ensure compatibility with newer Python versions and Pyramid releases.
Warnings
- breaking As of Pyramid 1.5, Mako templating support was removed from the Pyramid core. Projects upgrading to Pyramid 1.5 or newer must explicitly install `pyramid_mako` and activate it via `config.include('pyramid_mako')` in their application's `__init__.py` (or similar configuration entry point). Failure to do so will result in `ValueError: No such renderer factory .mako`.
- deprecated Returning a `('defname', dict)` tuple from a view using a Mako renderer is deprecated and will raise a `ValueError` in `pyramid_mako` 1.0+. This behavior was removed to standardize renderer usage.
- gotcha Setting `mako.strict_undefined = true` in your Pyramid application settings can cause issues with the `pyramid_debugtoolbar` if its internal Mako templates contain unguarded placeholders that might be undefined in certain contexts.
- gotcha As of Pyramid 1.4, spaces are no longer allowed in Mako template paths when rendering. Template paths containing spaces will lead to lookup failures.
Install
-
pip install pyramid-mako
Imports
- includeme
config.include('pyramid_mako') - render
from pyramid.renderers import render
- add_mako_renderer
config.add_mako_renderer('.html', settings_prefix='mako.')
Quickstart
import os
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
# --- myapp/__init__.py ---
@view_config(route_name='home', renderer='templates/home.mako')
def home_view(request):
name = request.matchdict.get('name', 'World')
return {'project': 'Pyramid Mako App', 'user_name': name}
def main(global_config, **settings):
with Configurator(settings=settings) as config:
config.include('pyramid_mako')
# Configure mako.directories for template lookup if using relative paths
# For this example, 'templates' is relative to the current package.
# If using asset specs like 'mypackage:templates', no mako.directories needed for that specific template.
config.add_settings({'mako.directories': 'myapp:templates'})
config.add_route('home', '/howdy/{name}')
config.add_route('root', '/')
config.scan('.') # Scan current package for @view_config decorators
return config.make_wsgi_app()
# To run: Save this as myapp/__init__.py
# Create myapp/templates/home.mako:
# <h1>Hello, ${user_name} from ${project}!</h1>
# From your project root (one level above 'myapp'), run:
# pserve development.ini (assuming development.ini is configured to point to myapp.main)
# Or create a minimal development.ini:
# [app:main]
# use = egg:myapp#main
#
# [server:main]
# use = egg:waitress#main
# host = 0.0.0.0
# port = 6543
#
# [loggers]
# keys = root, myapp
#
# [handlers]
# keys = console
#
# [formatters]
# keys = generic
#
# [logger_root]
# level = INFO
# handlers = console
#
# [logger_myapp]
# level = DEBUG
# handlers = console
# qualname = myapp
#
# [handler_console]
# class = StreamHandler
# args = (sys.stderr,)
# level = NOTSET
# formatter = generic
#
# [formatter_generic]
# format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s
if __name__ == '__main__':
# This block is typically replaced by 'pserve development.ini'
# For a truly minimal runnable example without an .ini file or folder structure:
class RootView:
def __init__(self, request):
self.request = request
@view_config(route_name='hello', renderer='string:<h1>Hello, ${user_name}!</h1>')
def hello(self):
return {'user_name': 'Anonymous'}
with Configurator() as config:
config.include('pyramid_mako')
config.add_route('hello', '/')
config.add_view(RootView, attr='hello')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 6543, app)
print('Serving on http://0.0.0.0:6543')
server.serve_forever()