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 |
|
84aa10
|
25 |
#. In this step let's start by copying the ``view_class`` step's directory from a few steps ago. |
b1b922
|
26 |
|
84aa10
|
27 |
.. code-block:: bash |
b1b922
|
28 |
|
84aa10
|
29 |
cd ..; cp -r view_classes jinja2; cd jinja2 |
b1b922
|
30 |
|
84aa10
|
31 |
#. Add ``pyramid_jinja2`` to our project's dependencies in ``setup.py``: |
b1b922
|
32 |
|
84aa10
|
33 |
.. literalinclude:: jinja2/setup.py |
SP |
34 |
:language: python |
|
35 |
:linenos: |
beeb8e
|
36 |
:emphasize-lines: 8 |
b1b922
|
37 |
|
84aa10
|
38 |
#. Install our project and its newly added dependency. |
b1b922
|
39 |
|
84aa10
|
40 |
.. code-block:: bash |
b1b922
|
41 |
|
84aa10
|
42 |
$VENV/bin/pip install -e . |
b1b922
|
43 |
|
84aa10
|
44 |
#. We need to include ``pyramid_jinja2`` in ``jinja2/tutorial/__init__.py``: |
b1b922
|
45 |
|
84aa10
|
46 |
.. literalinclude:: jinja2/tutorial/__init__.py |
SP |
47 |
:linenos: |
b1b922
|
48 |
|
84aa10
|
49 |
#. Our ``jinja2/tutorial/views.py`` simply changes its ``renderer``: |
b1b922
|
50 |
|
84aa10
|
51 |
.. literalinclude:: jinja2/tutorial/views.py |
SP |
52 |
:linenos: |
b1b922
|
53 |
|
84aa10
|
54 |
#. Add ``jinja2/tutorial/home.jinja2`` as a template: |
b1b922
|
55 |
|
84aa10
|
56 |
.. literalinclude:: jinja2/tutorial/home.jinja2 |
SP |
57 |
:language: html |
b1b922
|
58 |
|
84aa10
|
59 |
#. Now run the tests: |
b1b922
|
60 |
|
84aa10
|
61 |
.. code-block:: bash |
SP |
62 |
|
|
63 |
$VENV/bin/pytest tutorial/tests.py -q |
|
64 |
.... |
|
65 |
4 passed in 0.40 seconds |
|
66 |
|
|
67 |
#. Run your Pyramid application with: |
|
68 |
|
|
69 |
.. code-block:: bash |
|
70 |
|
|
71 |
$VENV/bin/pserve development.ini --reload |
|
72 |
|
|
73 |
#. Open http://localhost:6543/ in your browser. |
b1b922
|
74 |
|
d5e676
|
75 |
|
b1b922
|
76 |
Analysis |
PE |
77 |
======== |
|
78 |
|
d5e676
|
79 |
Getting a Pyramid add-on into Pyramid is simple. First you use normal Python |
SP |
80 |
package installation tools to install the add-on package into your Python |
|
81 |
virtual environment. You then tell Pyramid's configurator to run the setup code |
b1b922
|
82 |
in the add-on. In this case the setup code told Pyramid to make a new |
PE |
83 |
"renderer" available that looked for ``.jinja2`` file extensions. |
|
84 |
|
d5e676
|
85 |
Our view code stayed largely the same. We simply changed the file extension on |
SP |
86 |
the renderer. For the template, the syntax for Chameleon and Jinja2's basic |
|
87 |
variable insertion is very similar. |
b1b922
|
88 |
|
d5e676
|
89 |
|
SP |
90 |
Extra credit |
b1b922
|
91 |
============ |
PE |
92 |
|
d5e676
|
93 |
#. Our project now depends on ``pyramid_jinja2``. We installed that dependency |
SP |
94 |
manually. What is another way we could have made the association? |
b1b922
|
95 |
|
872796
|
96 |
#. We used ``config.include`` which is an imperative configuration to get the |
d5e676
|
97 |
:term:`Configurator` to load ``pyramid_jinja2``'s configuration. What is |
75a098
|
98 |
another way we could include it into the config? |
b1b922
|
99 |
|
d5e676
|
100 |
.. seealso:: `Jinja2 homepage <http://jinja.pocoo.org/>`_, and |
b1b922
|
101 |
:ref:`pyramid_jinja2 Overview <jinja2:overview>` |