Added templating docs. This basically fixes #92
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent fd06bcf · Browse files · 4 files·+200 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+200 added−1 removed
- other:+200 added−1 removed
Provenance
- Commit
4a2d2ba- Tree
f677156- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
fd06bcf
4 files changed+200 added−1 removed
modified:docs/contents.rst.inc+1 added
10 unmodified lines11 foreword12 installation13 quickstart14 tutorial/index15+ templating16 testing17 errorhandling18 config19 signals33 unmodified linesmodified:docs/quickstart.rst+2 added-1 removed
377 unmodified lines378 /templates379 /hello.html380 381 For templates you can use the full power of Jinja2 templates. Head over382+to the :ref:`templating` section of the documentation or the official382-to the `Jinja2 Template Documentation383+`Jinja2 Template Documentation384 <http://jinja.pocoo.org/2/documentation/templates>`_ for more information.385 386 Here an example template:387 361 unmodified linesmodified:docs/security.rst+9 added
3 unmodified lines4 Web applications usually face all kinds of security problems and it's very5 hard to get everything right. Flask tries to solve a few of these things6 for you, but there are a couple more you have to take care of yourself.7 8+.. _xss:9+10 Cross-Site Scripting (XSS)11 --------------------------12 13+Cross site scripting is the concept of injecting arbitrary HTML (and with14+it JavaScript) into the context of a website. To rememdy this, developers15+have to properly escape text so that it cannot include arbitrary HTML16+tags. For more information on that have a look at the Wikipedia article17+on `Cross-Site Scripting18+<http://en.wikipedia.org/wiki/Cross-site_scripting>`_.19+20 Flask configures Jinja2 to automatically escape all values unless21 explicitly told otherwise. This should rule out all XSS problems caused22 in templates, but there are still other places where you have to be23 careful:120 unmodified linesadded:docs/templating.rst+188 added
1+Templates2+=========3+4+Flask leverages Jinja2 as template engine. You are obviously free to use5+a different template engine, but you still have to install Jinja2 to run6+Flask itself. This requirement is necessary to enable rich extensions.7+An extension can depend on Jinja2 being present.8+9+This section only gives a very quick introduction into how Jinja210+is integrated into Flask. If you want information on the template11+engine's syntax itself, head over to the official `Jinja2 Template12+Documentation <http://jinja.pocoo.org/2/documentation/templates>`_ for13+more information.14+15+Jinja Setup16+-----------17+18+Unless customized, Jinja2 is configured by Flask as follows:19+20+- autoescaping is enabled for all templates ending in ``.html``,21+ ``.htm``, ``.xml`` as well as ``.xhtml``22+- a template has the ability to opt in/out autoescaping with the23+ ``{% autoescape %}`` tag.24+- Flask inserts a couple of global functions and helpers into the25+ Jinja2 context, additionally to the values that are present by26+ default.27+28+Standard Context29+----------------30+31+The following global variables are available within Jinja2 templates32+by default:33+34+.. data:: config35+ :noindex:36+37+ The current configuration object (:data:`flask.config`)38+39+ .. versionadded:: 0.640+41+.. data:: request42+ :noindex:43+44+ The current request object (:class:`flask.request`)45+46+.. data:: session47+ :noindex:48+49+ The current session object (:class:`flask.session`)50+51+.. data:: g52+ :noindex:53+54+ The request-bound object for global variables (:data:`flask.g`)55+56+.. function:: url_for57+ :noindex:58+59+ The :func:`flask.url_for` function.60+61+.. function:: get_flashed_messages62+ :noindex:63+64+ The :func:`flask.get_flashed_messages` function.65+66+.. admonition:: The Jinja Context Behaviour67+68+ These variables are added to the context of variables, they are not69+ global variables. The difference is that by default these will not70+ show up in the context of imported templates. This is partially caused71+ by performance considerations, partially to keep things explicit.72+73+ What does this mean for you? If you have a macro you want to import,74+ that needs to access the request object you have two possibilities:75+76+ 1. you explicitly pass the request to the macro as parameter, or77+ the attribute of the request object you are interested in.78+ 2. you import the macro "with context".79+80+ Importing with context looks like this:81+82+ .. sourcecode:: jinja83+84+ {% from '_helpers.html' import my_macro with context %}85+86+Standard Filters87+----------------88+89+These filters are available in Jinja2 additionally to the filters provided90+by Jinja2 itself:91+92+.. function:: tojson93+ :noindex:94+95+ This function converts the given object into JSON representation. This96+ is for example very helpful if you try to generate JavaScript on the97+ fly.98+99+ Note that inside `script` tags no escaping must take place, so make100+ sure to disable escaping with ``|safe`` if you intend to use it inside101+ `script` tags:102+103+ .. sourcecode:: html+jinja104+105+ <script type=text/javascript>106+ doSomethingWith({{ user.username|tojson|safe }});107+ </script>108+109+ That the ``|tojson`` filter escapes forward slashes properly for you.110+111+Controlling Autoescaping112+------------------------113+114+Autoescaping is the concept of automatically escaping special characters115+of you. Special characters in the sense of HTML (or XML, and thus XHTML)116+are ``&``, ``>``, ``<``, ``"`` as well as ``'``. Because these characters117+carry specific meanings in documents on their own you have to replace them118+by so called "entities" if you want to use them for text. Not doing so119+would not only cause user frustration by the inability to use these120+characters in text, but can also lead to security problems. (see121+:ref:`xss`)122+123+Sometimes however you will need to disable autoescaping in templates.124+This can be the case if you want to explicitly inject HTML into pages, for125+example if they come from a system that generate secure HTML like a126+markdown to HTML converter.127+128+There are three ways to accomplish that:129+130+- In the Python code, wrap the HTML string in a :class:`~flask.Markup`131+ object before passing it to the template. This is in general the132+ recommended way.133+- Inside the template, use the ``|safe`` filter to explicitly mark a134+ string as safe HTML (``{{ myvariable|safe }}``)135+- Temporarily disable the autoescape system altogether.136+137+To disable the autoescape system in templates, you can use the ``{%138+autoescape %}`` block:139+140+.. sourcecode:: html+jinja141+142+ {% autoescape false %}143+ <p>autoescaping is disabled here144+ <p>{{ will_not_be_escaped }}145+ {% endautoescape %}146+147+Whenever you do this, please be very cautious about the varibles you are148+using in this block.149+150+Registering Filters151+-------------------152+153+If you want to register your own filters in Jinja2 you have two ways to do154+that. You can either put them by hand into the155+:attr:`~flask.Flask.jinja_env` of the application or use the156+:meth:`~flask.Flask.template_filter` decorator.157+158+The two following examples work the same and both reverse an object::159+160+ @app.template_filter('reverse')161+ def reverse_filter(s):162+ return s[::-1]163+164+ def reverse_filter(s):165+ return s[::-1]166+ app.jinja_env.filters['reverse'] = reverse_filter167+168+In case of the decorator the argument is optional if you want to use the169+function name as name of the filter.170+171+Context Processors172+------------------173+174+To inject new variables automatically into the context of a template175+context processors exist in Flask. Context processors run before the176+template is rendered and have the ability to inject new values into the177+template context. A context processor is a function that returns a178+dictionary. The keys and values of this dictionary are then merged with179+the template context::180+181+ @app.context_processor182+ def inject_user():183+ return dict(user=g.user)184+185+The context processor above makes a variable called `user` available in186+the template with the value of `g.user`. This example is not very187+interesting because `g` is available in templates anyways, but it gives an188+idea how this works.