Skip to content
Myr

templates.rst

This file

Path

docs/tutorial/templates.rst

112 lines · 3.6 KiB

Last changed by

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

History of this file

Take it

The walk stopped after 200 commits: lines older than 54def62 point at it.

Merge pull request #1234 from Winnetou/patch-2Markus Unterwaditzerbefore adoption54def62

lines 1–2why, one step back
1.. _tutorial-templates:
2

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

line 3why, one step back
3Step 8: The Templates

Merge pull request #1234 from Winnetou/patch-2Markus Unterwaditzerbefore adoption54def62

lines 4–5why, one step back
4=====================
5

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

lines 6–9why, one step back
6Now it is time to start working on the templates. As you may have
7noticed, if you make requests with the app running, you will get8an exception that Flask cannot find the templates. The templates9are using `Jinja2`_ syntax and have autoescaping enabled by

Merge pull request #1234 from Winnetou/patch-2Markus Unterwaditzerbefore adoption54def62

lines 10–17why, one step back
10default. This means that unless you mark a value in the code with
11:class:`~flask.Markup` or with the ``|safe`` filter in the template,12Jinja2 will ensure that special characters such as ``<`` or ``>`` are13escaped with their XML equivalents.1415We are also using template inheritance which makes it possible to reuse16the layout of the website in all pages.17

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 18why, one step back
18Put the following templates into the :file:`templates` folder:

Merge pull request #1234 from Winnetou/patch-2Markus Unterwaditzerbefore adoption54def62

lines 19–58why, one step back
19
20.. _Jinja2: http://jinja.pocoo.org/docs/templates2122layout.html23-----------2425This template contains the HTML skeleton, the header and a link to log in26(or log out if the user was already logged in). It also displays the27flashed messages if there are any. The ``{% block body %}`` block can be28replaced by a block of the same name (``body``) in a child template.2930The :class:`~flask.session` dict is available in the template as well and31you can use that to check if the user is logged in or not. Note that in32Jinja you can access missing attributes and items of objects / dicts which33makes the following code work, even if there is no ``'logged_in'`` key in34the session:3536.. sourcecode:: html+jinja3738 <!doctype html>39 <title>Flaskr</title>40 <link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">41 <div class=page>42 <h1>Flaskr</h1>43 <div class=metanav>44 {% if not session.logged_in %}45 <a href="{{ url_for('login') }}">log in</a>46 {% else %}47 <a href="{{ url_for('logout') }}">log out</a>48 {% endif %}49 </div>50 {% for message in get_flashed_messages() %}51 <div class=flash>{{ message }}</div>52 {% endfor %}53 {% block body %}{% endblock %}54 </div>5556show_entries.html57-----------------58

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

lines 59–60why, one step back
59This template extends the :file:`layout.html` template from above to display the
60messages. Note that the ``for`` loop iterates over the messages we passed

Address #1902: Converts example/flaskr to have a setup.py (#1945)Kyle Lawlorbefore adoption17d4cb3

lines 61–63why, one step back
61in with the :func:`~flask.render_template` function. Notice that the form is
62configured to to submit to the `add_entry` view function and use ``POST`` as63HTTP method:

Merge pull request #1234 from Winnetou/patch-2Markus Unterwaditzerbefore adoption54def62

lines 64–91why, one step back
64
65.. sourcecode:: html+jinja6667 {% extends "layout.html" %}68 {% block body %}69 {% if session.logged_in %}70 <form action="{{ url_for('add_entry') }}" method=post class=add-entry>71 <dl>72 <dt>Title:73 <dd><input type=text size=30 name=title>74 <dt>Text:75 <dd><textarea name=text rows=5 cols=40></textarea>76 <dd><input type=submit value=Share>77 </dl>78 </form>79 {% endif %}80 <ul class=entries>81 {% for entry in entries %}82 <li><h2>{{ entry.title }}</h2>{{ entry.text|safe }}83 {% else %}84 <li><em>Unbelievable. No entries here so far</em>85 {% endfor %}86 </ul>87 {% endblock %}8889login.html90----------91

Merge pull request #1277 from iKevinY/doc-fixesMarkus Unterwaditzerbefore adoption82d7c49

line 92why, one step back
92This is the login template, which basically just displays a form to allow

Merge pull request #1234 from Winnetou/patch-2Markus Unterwaditzerbefore adoption54def62

lines 93–112why, one step back
93the user to login:
9495.. sourcecode:: html+jinja9697 {% extends "layout.html" %}98 {% block body %}99 <h2>Login</h2>100 {% if error %}<p class=error><strong>Error:</strong> {{ error }}{% endif %}101 <form action="{{ url_for('login') }}" method=post>102 <dl>103 <dt>Username:104 <dd><input type=text name=username>105 <dt>Password:106 <dd><input type=password name=password>107 <dd><input type=submit value=Login>108 </dl>109 </form>110 {% endblock %}111112Continue with :ref:`tutorial-css`.