Started working on refactoring Jinja integration
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 a154c87 · Browse files · 5 files·+54 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+54 added−8 removed
- tests:+9 added−0 removed
- other:+45 added−8 removed
Provenance
- Commit
7599046- Tree
156c8da- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
a154c87
5 files changed+54 added−8 removed
modified:docs/quickstart.rst+7 added
412 unmodified lines413 Markup(u'<blink>hacker</blink>')414 >>> Markup('<em>Marked up</em> » HTML').striptags()415 u'Marked up \xbb HTML'416 417+.. versionchanged:: 0.5418+419+ Autoescaping is no longer enabled for all templates. The following420+ extensions for templates trigger autoescaping: ``.html``, ``.htm``,421+ ``.xml``, ``.xhtml``. Templates loaded from string will have422+ autoescaping disabled.423+424 .. [#] Unsure what that :class:`~flask.g` object is? It's something you425 can store information on yourself, check the documentation of that426 object (:class:`~flask.g`) and the :ref:`sqlite3` for more427 information.303 unmodified linesadded:tests/templates/mail.txt+1 added
1+{{ foo}} Mailmodified:tests/flask_tests.py+8 added
459 unmodified lines460 '<p>Hello World!',461 '<p>Hello World!'462 ]463 464+ def test_no_escaping(self):465+ app = flask.Flask(__name__)466+ with app.test_request_context():467+ assert flask.render_template_string('{{ foo }}',468+ foo='<test>') == '<test>'469+ assert flask.render_template('mail.txt', foo='<test>') \470+ == '<test> Mail'471+472 def test_macros(self):473 app = flask.Flask(__name__)474 with app.test_request_context():475 macro = flask.get_template_attribute('_macro.html', 'hello')423 unmodified linesmodified:CHANGES+4 added
9 unmodified lines10 11 - fixed a bug with subdomains that was caused by the inability to12 specify the server name. The server name can now be set with13 the `SERVER_NAME` config key.14+- autoescaping is no longer active for all templates. Instead it15+ is only active for ``.html``, ``.htm``, ``.xml`` and ``.xhtml``.16+ Inside templates this behaviour can be changed with the17+ ``autoescape`` tag.18 19 Version 0.420 -----------21 66 unmodified linesmodified:flask.py+34 added-8 removed
786 unmodified lines787 def __repr__(self):788 return '<%s %s>' % (self.__class__.__name__, dict.__repr__(self))789 790 791+def _select_autoescape(filename):792+ """Returns `True` if autoescaping should be active for the given793+ template name.794+ """795+ if filename is None:796+ return False797+ return filename.endswith(('.html', '.htm', '.xml', '.xhtml'))798+799+800 class Flask(_PackageBoundObject):801 """The flask object implements a WSGI application and acts as the central802 object. It is passed the name of the module or package of the803 application. Once it is created it will act as a central registry for124 unmodified lines928 )929 930 #: Options that are passed directly to the Jinja2 environment.931 jinja_options = ImmutableDict(923- autoescape=True,932+ autoescape=_select_autoescape,933 extensions=['jinja2.ext.autoescape', 'jinja2.ext.with_']934 )935 936 #: Default configuration parameters.89 unmodified lines1026 1027 #: The Jinja2 environment. It is created from the1028 #: :attr:`jinja_options` and the loader that is returned1029 #: by the :meth:`create_jinja_loader` function.1021- self.jinja_env = Environment(loader=self.create_jinja_loader(),1022- **self.jinja_options)1023- self.jinja_env.globals.update(1024- url_for=url_for,1025- get_flashed_messages=get_flashed_messages1026- )1027- self.jinja_env.filters['tojson'] = _tojson_filter1030+ self.jinja_env = self.create_jinja_environment()1031+ self.init_jinja_globals()1032 1033 @property1034 def logger(self):1035 """A :class:`logging.Logger` object for this application. The28 unmodified lines1064 logger.addHandler(handler)1065 self._logger = logger1066 return logger1067 1068+ def create_jinja_environment(self):1069+ """Creates the Jinja2 environment based on :attr:`jinja_options`1070+ and :meth:`create_jinja_loader`.1071+1072+ .. versionadded:: 0.51073+ """1074+ return Environment(loader=self.create_jinja_loader(),1075+ **self.jinja_options)1076+1077 def create_jinja_loader(self):1078 """Creates the Jinja loader. By default just a package loader for1079 the configured package is returned that looks up templates in the1080 `templates` folder. To add other loaders it's possible to2 unmodified lines1083 if pkg_resources is None:1084 return FileSystemLoader(os.path.join(self.root_path, 'templates'))1085 return PackageLoader(self.import_name)1086 1087+ def init_jinja_globals(self):1088+ """Callde directly after the environment was created to inject1089+ some defaults (like `url_for`, `get_flashed_messages` and the1090+ `tojson` filter.1091+1092+ .. versionadded:: 0.51093+ """1094+ self.jinja_env.globals.update(1095+ url_for=url_for,1096+ get_flashed_messages=get_flashed_messages1097+ )1098+ self.jinja_env.filters['tojson'] = _tojson_filter1099+1100 def update_template_context(self, context):1101 """Update the template context with some commonly used variables.1102 This injects request, session and g into the template context.1103 496 unmodified lines