Skip to content
Myr

templates.rst

This file

Path

docs/tutorial/templates.rst

107 lines · 3.5 KiB

Last changed by

New style for the documentation. Looks more like the website now.Armin Ronacherbefore adoptionbcf347f

History of this file

Take it

1Step 6: The Templates2=====================34Now we should start working on the templates.  If we request the URLs now5we would only get an exception that Flask cannot find the templates.  The6templates are using `Jinja2`_ syntax and have autoescaping enabled by7default.  This means that unless you mark a value in the code with8:class:`~flask.Markup` or with the ``|safe`` filter in the template,9Jinja2 will ensure that special characters such as ``<`` or ``>`` are10escaped with their XML equivalents.1112We are also using template inheritance which makes it possible to reuse13the layout of the website in all pages.1415Put the following templates into the `templates` folder:1617.. _Jinja2: http://jinja.pocoo.org/2/documentation/templates1819layout.html20-----------2122This template contains the HTML skeleton, the header and a link to log in23(or log out if the user was already logged in).  It also displays the24flashed messages if there are any.  The ``{% block body %}`` block can be25replaced by a block of the same name (``body``) in a child template.2627The :class:`~flask.session` dict is available in the template as well and28you can use that to check if the user is logged in or not.  Note that in29Jinja you can access missing attributes and items of objects / dicts which30makes the following code work, even if there is no ``'logged_in'`` key in31the session:3233.. sourcecode:: html+jinja3435    <!doctype html>36    <title>Flaskr</title>37    <link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">38    <div class=page>39      <h1>Flaskr</h1>40      <div class=metanav>41      {% if not session.logged_in %}42        <a href="{{ url_for('login') }}">log in</a>43      {% else %}44        <a href="{{ url_for('logout') }}">log out</a>45      {% endif %}46      </div>47      {% for message in get_flashed_messages() %}48        <div class=flash>{{ message }}</div>49      {% endfor %}50      {% block body %}{% endblock %}51    </div>5253show_entries.html54-----------------5556This template extends the `layout.html` template from above to display the57messages.  Note that the `for` loop iterates over the messages we passed58in with the :func:`~flask.render_template` function.  We also tell the59form to submit to your `add_entry` function and use `POST` as `HTTP`60method:6162.. sourcecode:: html+jinja6364    {% extends "layout.html" %}65    {% block body %}66      {% if session.logged_in %}67        <form action="{{ url_for('add_entry') }}" method=post class=add-entry>68          <dl>69            <dt>Title:70            <dd><input type=text size=30 name=title>71            <dt>Text:72            <dd><textarea name=text rows=5 cols=40></textarea>73            <dd><input type=submit value=Share>74          </dl>75        </form>76      {% endif %}77      <ul class=entries>78      {% for entry in entries %}79        <li><h2>{{ entry.title }}</h2>{{ entry.text|safe }}80      {% else %}81        <li><em>Unbelievable.  No entries here so far</em>82      {% endfor %}83      </ul>84    {% endblock %}8586login.html87----------8889Finally the login template which basically just displays a form to allow90the user to login:9192.. sourcecode:: html+jinja9394    {% extends "layout.html" %}95    {% block body %}96      <h2>Login</h2>97      {% if error %}<p class=error><strong>Error:</strong> {{ error }}{% endif %}98      <form action="{{ url_for('login') }}" method=post>99        <dl>100          <dt>Username:101          <dd><input type=text name=username>102          <dt>Password:103          <dd><input type=password name=password>104          <dd><input type=submit value=Login>105        </dl>106      </form>107    {% endblock %}