12: Templating With
We just said Pyramid doesn't prefer one templating language over
another. Time to prove it. Jinja2 is a popular templating system,
used in Flask and modeled after Django's templates. Let's add
pyramid_jinja2, a Pyramid :term:`add-on` which enables Jinja2 as a
:term:`renderer` in our Pyramid applications.
- Show Pyramid's support for different templating systems
- Learn about installing Pyramid add-ons
In this step let's start by copying the
view_classstep's directory, and then installing the
$ cd ..; cp -r view_classes jinja2; cd jinja2 $ $VENV/bin/python setup.py develop $ $VENV/bin/easy_install pyramid_jinja2
We need to include
jinja2/tutorial/views.pysimply changes its
jinja2/tutorial/home.jinja2as a template:
Now run the tests:
$ $VENV/bin/nosetests tutorial
Run your Pyramid application with:
$ $VENV/bin/pserve development.ini --reload
Open http://localhost:6543/ in your browser.
Getting a Pyramid add-on into Pyramid is simple. First you use normal
Python package installation tools to install the add-on package into
your Python. You then tell Pyramid's configurator to run the setup code
in the add-on. In this case the setup code told Pyramid to make a new
"renderer" available that looked for
.jinja2 file extensions.
Our view code stayed largely the same. We simply changed the file extension on the renderer. For the template, the syntax for Chameleon and Jinja2's basic variable insertion is very similar.
- Our project now depends on
pyramid_jinja2. We installed that dependency manually. What is another way we could have made the association?
- We used
config.includewhich is an imperative configuration to get the :term:`Configurator` to load
pyramid_jinja2's configuration. What is another way could include it into the config?