Skip to content
Myr

use Jinja name consistently

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by David Lord · parent 38b4c1e · Browse files · 9 files+31 added−31 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+31 added−31 removed

  • other:+31 added−31 removed

9 files+31 added−31 removed

modified:docs/patterns/streaming.rst+1 added-1 removed
27 unmodified lines
2829Streaming from Templates30------------------------3132The Jinja2 template engine supports rendering a template piece by32The Jinja template engine supports rendering a template piece by33piece, returning an iterator of strings. Flask provides the34:func:`~flask.stream_template` and :func:`~flask.stream_template_string`35functions to make this easier to use.36
49 unmodified lines
modified:docs/patterns/wtforms.rst+1 added-1 removed
97 unmodified lines
98WTForm's field function, which renders the field for us. The keyword99arguments will be inserted as HTML attributes. So, for example, you can100call ``render_field(form.username, class='username')`` to add a class to101the input element. Note that WTForms returns standard Python strings,102so we have to tell Jinja2 that this data is already HTML-escaped with102so we have to tell Jinja that this data is already HTML-escaped with103the ``|safe`` filter.104105Here is the :file:`register.html` template for the function we used above, which106takes advantage of the :file:`_formhelpers.html` template:
20 unmodified lines
modified:docs/design.rst+8 added-8 removed
94 unmodified lines
9596One Template Engine97-------------------9899Flask decides on one template engine: Jinja2. Why doesn't Flask have a99Flask decides on one template engine: Jinja. Why doesn't Flask have a100pluggable template engine interface? You can obviously use a different101template engine, but Flask will still configure Jinja2 for you. While102that limitation that Jinja2 is *always* configured will probably go away,101template engine, but Flask will still configure Jinja for you. While102that limitation that Jinja is *always* configured will probably go away,103the decision to bundle one template engine and use that will not.104105Template engines are like programming languages and each of those engines106has a certain understanding about how things work. On the surface they107all work the same: you tell the engine to evaluate a template with a set108of variables and take the return value as string.109110But that's about where similarities end. Jinja2 for example has an110But that's about where similarities end. Jinja for example has an111extensive filter system, a certain way to do template inheritance,112support for reusable blocks (macros) that can be used from inside113templates and also from Python code, supports iterative template114rendering, configurable syntax and more. On the other hand an engine
2 unmodified lines
117other hand treats templates similar to Python modules.118119When it comes to connecting a template engine with an application or120framework there is more than just rendering templates. For instance,121Flask uses Jinja2's extensive autoescaping support. Also it provides122ways to access macros from Jinja2 templates.121Flask uses Jinja's extensive autoescaping support. Also it provides122ways to access macros from Jinja templates.123124A template abstraction layer that would not take the unique features of125the template engines away is a science on its own and a too large126undertaking for a microframework like Flask.
22 unmodified lines
149authentication technologies, and more. Flask may be "micro", but it's ready for150production use on a variety of needs.151152Why does Flask call itself a microframework and yet it depends on two153libraries (namely Werkzeug and Jinja2). Why shouldn't it? If we look153libraries (namely Werkzeug and Jinja). Why shouldn't it? If we look154over to the Ruby side of web development there we have a protocol very155similar to WSGI. Just that it's called Rack there, but besides that it156looks very much like a WSGI rendition for Ruby. But nearly all157applications in Ruby land do not work with Rack directly, but on top of a
49 unmodified lines
207--------------------------------208209Flask will never have a database layer. It will not have a form library210or anything else in that direction. Flask itself just bridges to Werkzeug211to implement a proper WSGI application and to Jinja2 to handle templating.211to implement a proper WSGI application and to Jinja to handle templating.212It also binds to a few common standard library packages such as logging.213Everything else is up for extensions.214215Why is this the case? Because people have different preferences and
13 unmodified lines
modified:docs/quickstart.rst+3 added-3 removed
350 unmodified lines
351-------------------352353Generating HTML from within Python is not fun, and actually pretty354cumbersome because you have to do the HTML escaping on your own to keep355the application secure. Because of that Flask configures the `Jinja2355the application secure. Because of that Flask configures the `Jinja356<https://palletsprojects.com/p/jinja/>`_ template engine for you automatically.357358Templates can be used to generate any type of text file. For web applications, you'll359primarily be generating HTML pages, but you can also generate markdown, plain text for
31 unmodified lines
391 /__init__.py392 /templates393 /hello.html394395For templates you can use the full power of Jinja2 templates. Head over396to the official `Jinja2 Template Documentation395For templates you can use the full power of Jinja templates. Head over396to the official `Jinja Template Documentation397<https://jinja.palletsprojects.com/templates/>`_ for more information.398399Here is an example template:400
505 unmodified lines
modified:docs/templating.rst+11 added-11 removed
1Templates2=========34Flask leverages Jinja2 as its template engine. You are obviously free to use5a different template engine, but you still have to install Jinja2 to run4Flask leverages Jinja as its template engine. You are obviously free to use5a different template engine, but you still have to install Jinja to run6Flask itself. This requirement is necessary to enable rich extensions.7An extension can depend on Jinja2 being present.7An extension can depend on Jinja being present.89This section only gives a very quick introduction into how Jinja29This section only gives a very quick introduction into how Jinja10is integrated into Flask. If you want information on the template11engine's syntax itself, head over to the official `Jinja2 Template11engine's syntax itself, head over to the official `Jinja Template12Documentation <https://jinja.palletsprojects.com/templates/>`_ for13more information.1415Jinja Setup16-----------1718Unless customized, Jinja2 is configured by Flask as follows:18Unless customized, Jinja is configured by Flask as follows:1920- autoescaping is enabled for all templates ending in ``.html``,21 ``.htm``, ``.xml``, ``.xhtml``, as well as ``.svg`` when using22 :func:`~flask.templating.render_template`.
1 unmodified line
24 :func:`~flask.templating.render_template_string`.25- a template has the ability to opt in/out autoescaping with the26 ``{% autoescape %}`` tag.27- Flask inserts a couple of global functions and helpers into the28 Jinja2 context, additionally to the values that are present by28 Jinja context, additionally to the values that are present by29 default.3031Standard Context32----------------3334The following global variables are available within Jinja2 templates34The following global variables are available within Jinja templates35by default:3637.. data:: config38 :noindex:
100 unmodified lines
139140Registering Filters141-------------------142143If you want to register your own filters in Jinja2 you have two ways to do143If you want to register your own filters in Jinja you have two ways to do144that. You can either put them by hand into the145:attr:`~flask.Flask.jinja_env` of the application or use the146:meth:`~flask.Flask.template_filter` decorator.147
8 unmodified lines
156 app.jinja_env.filters['reverse'] = reverse_filter157158In case of the decorator the argument is optional if you want to use the159function name as name of the filter. Once registered, you can use the filter160in your templates in the same way as Jinja2's builtin filters, for example if160in your templates in the same way as Jinja's builtin filters, for example if161you have a Python list in context called `mylist`::162163 {% for x in mylist | reverse %}164 {% endfor %}
45 unmodified lines
210strings. This can be used for streaming HTML in chunks to speed up211initial page load, or to save memory when rendering a very large212template.213214The Jinja2 template engine supports rendering a template piece214The Jinja template engine supports rendering a template piece215by piece, returning an iterator of strings. Flask provides the216:func:`~flask.stream_template` and :func:`~flask.stream_template_string`217functions to make this easier to use.218
11 unmodified lines
modified:docs/web-security.rst+3 added-3 removed
49 unmodified lines
50tags. For more information on that have a look at the Wikipedia article51on `Cross-Site Scripting52<https://en.wikipedia.org/wiki/Cross-site_scripting>`_.5354Flask configures Jinja2 to automatically escape all values unless54Flask configures Jinja to automatically escape all values unless55explicitly told otherwise. This should rule out all XSS problems caused56in templates, but there are still other places where you have to be57careful:5859- generating HTML without the help of Jinja259- generating HTML without the help of Jinja60- calling :class:`~markupsafe.Markup` on data submitted by users61- sending out HTML from uploaded files, never do that, use the62 ``Content-Disposition: attachment`` header to prevent that problem.63- sending out textfiles from uploaded files. Some browsers are using64 content-type guessing based on the first few bytes so users could65 trick a browser to execute HTML.6667Another thing that is very important are unquoted attributes. While68Jinja2 can protect you from XSS issues by escaping HTML, there is one68Jinja can protect you from XSS issues by escaping HTML, there is one69thing it cannot protect you from: XSS by attribute injection. To counter70this possible attack vector, be sure to always quote your attributes with71either double or single quotes when using Jinja expressions in them:72
244 unmodified lines
modified:src/flask/sansio/app.py+1 added-1 removed
519 unmodified lines
520 return os.path.join(package_path, "instance")521 return os.path.join(prefix, "var", f"{self.name}-instance")522523 def create_global_jinja_loader(self) -> DispatchingJinjaLoader:524 """Creates the loader for the Jinja2 environment. Can be used to524 """Creates the loader for the Jinja environment. Can be used to525 override just the loader and keeping the rest unchanged. It's526 discouraged to override this function. Instead one should override527 the :meth:`jinja_loader` function instead.528
436 unmodified lines
modified:src/flask/templating.py+1 added-1 removed
35 unmodified lines
36 return rv373839class Environment(BaseEnvironment):40 """Works like a regular Jinja2 environment but has some additional40 """Works like a regular Jinja environment but has some additional41 knowledge of how Flask's blueprint works so that it can prepend the42 name of the blueprint to referenced templates if necessary.43 """44
175 unmodified lines
modified:CHANGES.rst+2 added-2 removed
1374 unmodified lines
1375- The context preservation on exceptions is now an integral component1376 of Flask itself and no longer of the test client. This cleaned up1377 some internal logic and lowers the odds of runaway request contexts1378 in unittests.1379- Fixed the Jinja2 environment's ``list_templates`` method not1379- Fixed the Jinja environment's ``list_templates`` method not1380 returning the correct names when blueprints or modules were1381 involved.13821383
77 unmodified lines
1461Released 2010-12-3114621463- Fixed an issue where the default ``OPTIONS`` response was not1464 exposing all valid methods in the ``Allow`` header.1465- Jinja2 template loading syntax now allows "./" in front of a1465- Jinja template loading syntax now allows "./" in front of a1466 template load path. Previously this caused issues with module1467 setups.1468- Fixed an issue where the subdomain setting for modules was ignored1469 for the static folder.
155 unmodified lines