Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

codeA guest post by Steve Piercy, a freelance web application developer in Soquel, California, where he grows fruits and veggies, and rides his bicycle for everyday transportation. Steve gives introductions to Pyramid at SFPython Hack Night Meetups and PyLadiesSF South Bay Meetups, and frequently contributes to open source projects including Pyramid and Knop. Contact Steve at web@stevepiercy.com or as stevepiercy in the #pyramid channel on irc.freenode.net.

In this post I 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:

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

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 the 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.

Now let’s create a template using Chameleon. Create a new file myproject/hello_world/hello_world/templates/hello.py with the following code:

${variable_name or "Some text"} is a feature of Chameleon that will replace the value of name that we passed through the query parameter of the same name via a Python dictionary, or if no value exists for name, then the text will be displayed.

Now we’re ready to launch our application and see how the template works. But first, let’s do a quick review about reloading templates.

Automatically reloading templates during development

In my previous post, we learned how to monitor changes to files and automatically reload the application during development only through the application runner pserve. Here is that command, where $VENV is an environment variable set to your virtual environment’s path.

View the template

Now we can visit the URL in a web browser, http://localhost:6543/hello. You should see the text, “Hello, I love you. Won’t you tell me your name?” Now, append a query parameter to the URL, “?name=Jim Morrison“. You should see the text, “Hello, Jim Morrison”.

Conclusion

Templates make it easy for developers to generate user readable content for websites, performing replacements of placeholders. Pyramid supports bindings for the templating languages Jinja2, Mako, and Chameleon, so go ahead and try them out.

For more details about Python, see the Safari Books Online resources referenced below.

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

Learning Python, 5th Edition will help you quickly write efficient, high-quality code with Python. It’s an ideal way to begin, whether you’re new to programming or a professional developer versed in other languages.
Hello! Python fully covers the building blocks of Python programming and gives you a gentle introduction to more advanced topics such as object-oriented programming, functional programming, network programming, and program design. New (or nearly new) programmers will learn most of what they need to know to start using Python immediately.
Violent Python shows you how to move from a theoretical understanding of offensive computing concepts to a practical implementation. This book demonstrates how to write Python scripts to automate large-scale network attacks, extract metadata, and investigate forensic artifacts. It also shows how to write code to intercept and analyze network traffic using Python, craft and spoof wireless frames to attack wireless and Bluetooth devices, and how to data-mine popular social media websites and evade modern anti-virus.

Tags: Chameleon, Jinja2, Mako, Pyramid, Python, Template Tips, Templates,

Comments are closed.