Mako Template Bindings for Pyramid

1.1.0 · maintenance · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to set up a basic Pyramid application using `pyramid_mako`. It shows how to include `pyramid_mako` in your configuration, define a template directory using `mako.directories`, and render a Mako template from a Pyramid view. Views that use a renderer should return a dictionary, whose keys become top-level variables in the template. The example provides a small, self-contained Pyramid application. Note that for real-world applications, you'd typically use a `development.ini` file and a proper project structure created by a Pyramid cookiecutter.

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()

view raw JSON →