commit | author | age
|
b731b5
|
1 |
.. _qtut_templating: |
PE |
2 |
|
b1b922
|
3 |
=================================== |
PE |
4 |
08: HTML Generation With Templating |
|
5 |
=================================== |
|
6 |
|
3dece9
|
7 |
Most web frameworks don't embed HTML in programming code. Instead, they pass |
SP |
8 |
data into a templating system. In this step we look at the basics of using HTML |
|
9 |
templates in Pyramid. |
|
10 |
|
b1b922
|
11 |
|
PE |
12 |
Background |
|
13 |
========== |
|
14 |
|
3dece9
|
15 |
Ouch. We have been making our own ``Response`` and filling the response body |
SP |
16 |
with HTML. You usually won't embed an HTML string directly in Python, but |
|
17 |
instead will use a templating language. |
b1b922
|
18 |
|
3dece9
|
19 |
Pyramid doesn't mandate a particular database system, form library, and so on. |
SP |
20 |
It encourages replaceability. This applies equally to templating, which is |
|
21 |
fortunate: developers have strong views about template languages. As of |
|
22 |
Pyramid 1.5a2, Pyramid doesn't even bundle a template language! |
34e974
|
23 |
|
3dece9
|
24 |
It does, however, have strong ties to Jinja2, Mako, and Chameleon. In this step |
842a4f
|
25 |
we see how to add `pyramid_chameleon |
SP |
26 |
<https://github.com/Pylons/pyramid_chameleon>`_ to your project, then change |
|
27 |
your views to use templating. |
3dece9
|
28 |
|
b1b922
|
29 |
|
PE |
30 |
Objectives |
|
31 |
========== |
34e974
|
32 |
|
3dece9
|
33 |
- Enable the ``pyramid_chameleon`` Pyramid add-on. |
b1b922
|
34 |
|
3dece9
|
35 |
- Generate HTML from template files. |
b1b922
|
36 |
|
3dece9
|
37 |
- Connect the templates as "renderers" for view code. |
b1b922
|
38 |
|
3dece9
|
39 |
- Change the view code to simply return data. |
SP |
40 |
|
b1b922
|
41 |
|
PE |
42 |
Steps |
|
43 |
===== |
|
44 |
|
3dece9
|
45 |
#. Let's begin by using the previous package as a starting point for a new |
SP |
46 |
project: |
b1b922
|
47 |
|
PE |
48 |
.. code-block:: bash |
|
49 |
|
187104
|
50 |
$ cd ..; cp -r views templating; cd templating |
34e974
|
51 |
|
3dece9
|
52 |
#. This step depends on ``pyramid_chameleon``, so add it as a dependency in |
SP |
53 |
``templating/setup.py``: |
34e974
|
54 |
|
PE |
55 |
.. literalinclude:: templating/setup.py |
|
56 |
:linenos: |
|
57 |
|
|
58 |
#. Now we can activate the development-mode distribution: |
|
59 |
|
|
60 |
.. code-block:: bash |
|
61 |
|
96ca13
|
62 |
$ $VENV/bin/pip install -e . |
34e974
|
63 |
|
3dece9
|
64 |
#. We need to connect ``pyramid_chameleon`` as a renderer by making a call in |
SP |
65 |
the setup of ``templating/tutorial/__init__.py``: |
34e974
|
66 |
|
PE |
67 |
.. literalinclude:: templating/tutorial/__init__.py |
|
68 |
:linenos: |
b1b922
|
69 |
|
PE |
70 |
#. Our ``templating/tutorial/views.py`` no longer has HTML in it: |
|
71 |
|
|
72 |
.. literalinclude:: templating/tutorial/views.py |
|
73 |
:linenos: |
|
74 |
|
|
75 |
#. Instead we have ``templating/tutorial/home.pt`` as a template: |
|
76 |
|
|
77 |
.. literalinclude:: templating/tutorial/home.pt |
|
78 |
:language: html |
|
79 |
|
3dece9
|
80 |
#. For convenience, change ``templating/development.ini`` to reload templates |
SP |
81 |
automatically with ``pyramid.reload_templates``: |
b1b922
|
82 |
|
PE |
83 |
.. literalinclude:: templating/development.ini |
|
84 |
:language: ini |
|
85 |
|
3dece9
|
86 |
#. Our unit tests in ``templating/tutorial/tests.py`` can focus on data: |
b1b922
|
87 |
|
PE |
88 |
.. literalinclude:: templating/tutorial/tests.py |
|
89 |
:linenos: |
|
90 |
|
|
91 |
#. Now run the tests: |
|
92 |
|
|
93 |
.. code-block:: bash |
|
94 |
|
3dece9
|
95 |
$ $VENV/bin/py.test tutorial/tests.py -q |
SP |
96 |
.... |
|
97 |
4 passed in 0.46 seconds |
b1b922
|
98 |
|
PE |
99 |
#. Run your Pyramid application with: |
|
100 |
|
|
101 |
.. code-block:: bash |
|
102 |
|
187104
|
103 |
$ $VENV/bin/pserve development.ini --reload |
b1b922
|
104 |
|
3dece9
|
105 |
#. Open http://localhost:6543/ and http://localhost:6543/howdy in your browser. |
SP |
106 |
|
b1b922
|
107 |
|
PE |
108 |
Analysis |
|
109 |
======== |
|
110 |
|
3dece9
|
111 |
Ahh, that looks better. We have a view that is focused on Python code. Our |
SP |
112 |
``@view_config`` decorator specifies a :term:`renderer` that points to our |
|
113 |
template file. Our view then simply returns data which is then supplied to our |
|
114 |
template. Note that we used the same template for both views. |
b1b922
|
115 |
|
3dece9
|
116 |
Note the effect on testing. We can focus on having a data-oriented contract |
SP |
117 |
with our view code. |
b1b922
|
118 |
|
6625dd
|
119 |
.. seealso:: :ref:`templates_chapter`, :ref:`debugging_templates`, and |
SP |
120 |
:ref:`available_template_system_bindings`. |