.. _tutorial-templates: Step 7: The Templates ===================== Now it is time to start working on the templates. As you may have noticed, if you make requests with the app running, you will get an exception that Flask cannot find the templates. The templates are using `Jinja2`_ syntax and have autoescaping enabled by default. This means that unless you mark a value in the code with :class:`~flask.Markup` or with the ``|safe`` filter in the template, Jinja2 will ensure that special characters such as ``<`` or ``>`` are escaped with their XML equivalents. We are also using template inheritance which makes it possible to reuse the layout of the website in all pages. Put the following templates into the :file:`templates` folder: .. _Jinja2: http://jinja.pocoo.org/docs/templates layout.html ----------- This template contains the HTML skeleton, the header and a link to log in (or log out if the user was already logged in). It also displays the flashed messages if there are any. The ``{% block body %}`` block can be replaced by a block of the same name (``body``) in a child template. The :class:`~flask.session` dict is available in the template as well and you can use that to check if the user is logged in or not. Note that in Jinja you can access missing attributes and items of objects / dicts which makes the following code work, even if there is no ``'logged_in'`` key in the session: .. sourcecode:: html+jinja Flaskr

Flaskr

{% if not session.logged_in %} log in {% else %} log out {% endif %}
{% for message in get_flashed_messages() %}
{{ message }}
{% endfor %} {% block body %}{% endblock %}
show_entries.html ----------------- This template extends the :file:`layout.html` template from above to display the messages. Note that the ``for`` loop iterates over the messages we passed in with the :func:`~flask.render_template` function. Notice that the form is configured to to submit to the `add_entry` view function and use ``POST`` as HTTP method: .. sourcecode:: html+jinja {% extends "layout.html" %} {% block body %} {% if session.logged_in %}
Title:
Text:
{% endif %} {% endblock %} login.html ---------- This is the login template, which basically just displays a form to allow the user to login: .. sourcecode:: html+jinja {% extends "layout.html" %} {% block body %}

Login

{% if error %}

Error: {{ error }}{% endif %}

Username:
Password:
{% endblock %} Continue with :ref:`tutorial-css`.