commit | author | age
|
b731b5
|
1 |
.. _qtut_jinja2: |
PE |
2 |
|
b1b922
|
3 |
============================== |
PE |
4 |
12: Templating With ``jinja2`` |
|
5 |
============================== |
|
6 |
|
d5e676
|
7 |
We just said Pyramid doesn't prefer one templating language over another. Time |
SP |
8 |
to prove it. Jinja2 is a popular templating system, used in Flask and modeled |
|
9 |
after Django's templates. Let's add ``pyramid_jinja2``, a Pyramid |
|
10 |
:term:`add-on` which enables Jinja2 as a :term:`renderer` in our Pyramid |
|
11 |
applications. |
|
12 |
|
b1b922
|
13 |
|
PE |
14 |
Objectives |
|
15 |
========== |
|
16 |
|
d5e676
|
17 |
- Show Pyramid's support for different templating systems. |
b1b922
|
18 |
|
d5e676
|
19 |
- Learn about installing Pyramid add-ons. |
SP |
20 |
|
b1b922
|
21 |
|
PE |
22 |
Steps |
|
23 |
===== |
|
24 |
|
d5e676
|
25 |
#. In this step let's start by copying the ``view_class`` step's directory, |
SP |
26 |
and then installing the ``pyramid_jinja2`` add-on. |
b1b922
|
27 |
|
PE |
28 |
.. code-block:: bash |
|
29 |
|
187104
|
30 |
$ cd ..; cp -r view_classes jinja2; cd jinja2 |
45fabb
|
31 |
$ $VENV/bin/pip install -e . |
96ca13
|
32 |
$ $VENV/bin/pip install pyramid_jinja2 |
b1b922
|
33 |
|
d5e676
|
34 |
#. We need to include ``pyramid_jinja2`` in ``jinja2/tutorial/__init__.py``: |
b1b922
|
35 |
|
a2b158
|
36 |
.. literalinclude:: jinja2/tutorial/__init__.py |
b1b922
|
37 |
:linenos: |
PE |
38 |
|
|
39 |
#. Our ``jinja2/tutorial/views.py`` simply changes its ``renderer``: |
|
40 |
|
|
41 |
.. literalinclude:: jinja2/tutorial/views.py |
|
42 |
:linenos: |
|
43 |
|
|
44 |
#. Add ``jinja2/tutorial/home.jinja2`` as a template: |
|
45 |
|
|
46 |
.. literalinclude:: jinja2/tutorial/home.jinja2 |
|
47 |
:language: html |
|
48 |
|
|
49 |
#. Now run the tests: |
|
50 |
|
|
51 |
.. code-block:: bash |
|
52 |
|
d5e676
|
53 |
$ $VENV/bin/py.test tutorial/tests.py -q |
SP |
54 |
.... |
|
55 |
4 passed in 0.40 seconds |
b1b922
|
56 |
|
PE |
57 |
#. Run your Pyramid application with: |
|
58 |
|
|
59 |
.. code-block:: bash |
|
60 |
|
187104
|
61 |
$ $VENV/bin/pserve development.ini --reload |
b1b922
|
62 |
|
d749bf
|
63 |
#. Open http://localhost:6543/ in your browser. |
b1b922
|
64 |
|
d5e676
|
65 |
|
b1b922
|
66 |
Analysis |
PE |
67 |
======== |
|
68 |
|
d5e676
|
69 |
Getting a Pyramid add-on into Pyramid is simple. First you use normal Python |
SP |
70 |
package installation tools to install the add-on package into your Python |
|
71 |
virtual environment. You then tell Pyramid's configurator to run the setup code |
b1b922
|
72 |
in the add-on. In this case the setup code told Pyramid to make a new |
PE |
73 |
"renderer" available that looked for ``.jinja2`` file extensions. |
|
74 |
|
d5e676
|
75 |
Our view code stayed largely the same. We simply changed the file extension on |
SP |
76 |
the renderer. For the template, the syntax for Chameleon and Jinja2's basic |
|
77 |
variable insertion is very similar. |
b1b922
|
78 |
|
d5e676
|
79 |
|
SP |
80 |
Extra credit |
b1b922
|
81 |
============ |
PE |
82 |
|
d5e676
|
83 |
#. Our project now depends on ``pyramid_jinja2``. We installed that dependency |
SP |
84 |
manually. What is another way we could have made the association? |
b1b922
|
85 |
|
872796
|
86 |
#. We used ``config.include`` which is an imperative configuration to get the |
d5e676
|
87 |
:term:`Configurator` to load ``pyramid_jinja2``'s configuration. What is |
8f0744
|
88 |
another way we could include it into the config? |
b1b922
|
89 |
|
d5e676
|
90 |
.. seealso:: `Jinja2 homepage <http://jinja.pocoo.org/>`_, and |
b1b922
|
91 |
:ref:`pyramid_jinja2 Overview <jinja2:overview>` |