Template Tips in Pyramid

This article was originally published on Safari Books Online and can be read in its entirety at Template Tips in Pyramid. An abridged version follows.

Introduction

In this article we will introduce some tips for using template languages in Pyramid. A template language makes it easier for a developer to generate navigation for a website or to insert values into placeholders. Pyramid does not require any template language nor does it have any preference. In fact, in the release of Pyramid 1.5a2, Pyramid no longer bundles a template language. Instead Pyramid has add-on template system bindings for Jinja2, Mako, and Chameleon.

In our previous article, we manually created a response, and that would get tedious very quickly. Let's try an example of templating in Pyramid using Chameleon as a template language.

Templating using Chameleon

Building on our earlier work, and repeating what we did before, edit the file myproject/hello_world/hello_world/__init__.py, and add a new route to the application configuration as indicated on line 11.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
from pyramid.config import Configurator


def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(settings=settings)
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home', '/')
    config.add_route('aliens', '/aliens')
    config.add_route('hello', '/hello')
    config.scan()
    return config.make_wsgi_app()

Edit the file myproject/hello_world/hello_world/views.py, and add a new view using a decorator as indicated on lines 15-17.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from pyramid.view import view_config
from pyramid.response import Response


@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
    return {'project': 'hello_world'}

@view_config(route_name='aliens')
def aliens_view(request):
    action = request.params.get('action', ' do nothing')
    body = '<p>Aliens <strong>%s</strong>.</p>'
    return Response(body % action)

@view_config(route_name='hello', renderer='templates/hello.pt')
def hello(request):
    return dict(name=request.params.get('name'))

Line 15 adds a new argument renderer, which specifies the path to the template we are about to create relative to the directory that contains this file views.py. The path could also be absolute or be an asset specification in the form of my.dotted.package_name:relative/path.

Line 17 returns a Python dictionary into the template, where the key name is set to the value of the GET parameter name. This will become available as a variable in our template.

Continue Reading

Read the entire article Template Tips in Pyramid at Safari Books Online.

Did you like this article? Please send me a Gratipay, as little as 25¢ per week!

Updates, suggestions and comments regarding this article may be sent to Steve Piercy, [email protected] or comment using Disqus.


Written by Steve Piercy in Pyramid on Fri, Jan 17, 2014.
Last modified: Thu, Jun 19, 2014
Tags: Pyramid, Python, web framework, templating, Chameleon

Comments

comments powered by Disqus