diff options
Diffstat (limited to 'docs/quick_tutorial/templating.rst')
| -rw-r--r-- | docs/quick_tutorial/templating.rst | 128 |
1 files changed, 61 insertions, 67 deletions
diff --git a/docs/quick_tutorial/templating.rst b/docs/quick_tutorial/templating.rst index d73067f48..cef54fb6f 100644 --- a/docs/quick_tutorial/templating.rst +++ b/docs/quick_tutorial/templating.rst @@ -4,120 +4,114 @@ 08: HTML Generation With Templating =================================== -Most web frameworks don't embed HTML in programming code. Instead, -they pass data into a templating system. In this step we look at the -basics of using HTML templates in Pyramid. +Most web frameworks don't embed HTML in programming code. Instead, they pass +data into a templating system. In this step we look at the basics of using HTML +templates in Pyramid. + Background ========== -Ouch. We have been making our own ``Response`` and filling the response -body with HTML. You usually won't embed an HTML string directly in -Python, but instead, will use a templating language. +Ouch. We have been making our own ``Response`` and filling the response body +with HTML. You usually won't embed an HTML string directly in Python, but +instead will use a templating language. + +Pyramid doesn't mandate a particular database system, form library, and so on. +It encourages replaceability. This applies equally to templating, which is +fortunate: developers have strong views about template languages. As of +Pyramid 1.5a2, Pyramid doesn't even bundle a template language! -Pyramid doesn't mandate a particular database system, form library, -etc. It encourages replaceability. This applies equally to templating, -which is fortunate: developers have strong views about template -languages. As of Pyramid 1.5a2, Pyramid doesn't even bundle a template -language! +It does, however, have strong ties to Jinja2, Mako, and Chameleon. In this step +we see how to add `pyramid_chameleon +<https://github.com/Pylons/pyramid_chameleon>`_ to your project, then change +your views to use templating. -It does, however, have strong ties to Jinja2, Mako, and Chameleon. In -this step we see how to add ``pyramid_chameleon`` to your project, -then change your views to use templating. Objectives ========== -- Enable the ``pyramid_chameleon`` Pyramid add-on +- Enable the ``pyramid_chameleon`` Pyramid add-on. -- Generate HTML from template files +- Generate HTML from template files. -- Connect the templates as "renderers" for view code +- Connect the templates as "renderers" for view code. + +- Change the view code to simply return data. -- Change the view code to simply return data Steps ===== -#. Let's begin by using the previous package as a starting point for a - new project: - - .. code-block:: bash +#. Let's begin by using the previous package as a starting point for a new project: - $ cd ..; cp -r views templating; cd templating + .. code-block:: bash -#. This step depends on ``pyramid_chameleon``, so add it as a dependency - in ``templating/setup.py``: + cd ..; cp -r views templating; cd templating - .. literalinclude:: templating/setup.py - :linenos: +#. This step depends on ``pyramid_chameleon``, so add it as a dependency in ``templating/setup.py``: -#. Now we can activate the development-mode distribution: + .. literalinclude:: templating/setup.py + :linenos: + :emphasize-lines: 7 - .. code-block:: bash +#. Now we can activate the development-mode distribution: - $ $VENV/bin/python setup.py develop + .. code-block:: bash -#. We need to connect ``pyramid_chameleon`` as a renderer by making a - call in the setup of ``templating/tutorial/__init__.py``: + $VENV/bin/pip install -e . - .. literalinclude:: templating/tutorial/__init__.py - :linenos: +#. We need to connect ``pyramid_chameleon`` as a renderer by making a call in the setup of ``templating/tutorial/__init__.py``: -#. Our ``templating/tutorial/views.py`` no longer has HTML in it: + .. literalinclude:: templating/tutorial/__init__.py + :linenos: - .. literalinclude:: templating/tutorial/views.py - :linenos: +#. Our ``templating/tutorial/views.py`` no longer has HTML in it: -#. Instead we have ``templating/tutorial/home.pt`` as a template: + .. literalinclude:: templating/tutorial/views.py + :linenos: - .. literalinclude:: templating/tutorial/home.pt - :language: html +#. Instead we have ``templating/tutorial/home.pt`` as a template: -#. For convenience, change ``templating/development.ini`` to reload - templates automatically with ``pyramid.reload_templates``: + .. literalinclude:: templating/tutorial/home.pt + :language: html - .. literalinclude:: templating/development.ini - :language: ini +#. For convenience, change ``templating/development.ini`` to reload templates automatically with ``pyramid.reload_templates``: -#. Our unit tests in ``templating/tutorial/tests.py`` can focus on - data: + .. literalinclude:: templating/development.ini + :language: ini - .. literalinclude:: templating/tutorial/tests.py - :linenos: +#. Our unit tests in ``templating/tutorial/tests.py`` can focus on data: -#. Now run the tests: + .. literalinclude:: templating/tutorial/tests.py + :linenos: - .. code-block:: bash +#. Now run the tests: + .. code-block:: bash - $ $VENV/bin/nosetests tutorial - . - ---------------------------------------------------------------------- - Ran 4 tests in 0.141s + $VENV/bin/pytest tutorial/tests.py -q + .... + 4 passed in 0.46 seconds - OK +#. Run your Pyramid application with: -#. Run your Pyramid application with: + .. code-block:: bash - .. code-block:: bash + $VENV/bin/pserve development.ini --reload - $ $VENV/bin/pserve development.ini --reload +#. Open http://localhost:6543/ and http://localhost:6543/howdy in your browser. -#. Open http://localhost:6543/ and http://localhost:6543/howdy - in your browser. Analysis ======== -Ahh, that looks better. We have a view that is focused on Python code. -Our ``@view_config`` decorator specifies a :term:`renderer` that points -our template file. Our view then simply returns data which is then -supplied to our template. Note that we used the same template for both -views. +Ahh, that looks better. We have a view that is focused on Python code. Our +``@view_config`` decorator specifies a :term:`renderer` that points to our +template file. Our view then simply returns data which is then supplied to our +template. Note that we used the same template for both views. -Note the effect on testing. We can focus on having a data-oriented -contract with our view code. +Note the effect on testing. We can focus on having a data-oriented contract +with our view code. .. seealso:: :ref:`templates_chapter`, :ref:`debugging_templates`, and :ref:`available_template_system_bindings`. |
