10000 Moving our Jinja2 filter to ``flask_assets``. · woodcoder/flask-assets@3e3477f · GitHub
[go: up one dir, main page]

Skip to content

Commit 3e3477f

Browse files
Moving our Jinja2 filter to flask_assets.
And having it override the build-in ``jinja2`` template, so that we get the included Flask contexts by default.
1 parent c6fe73a commit 3e3477f

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

src/filter.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/flask_assets.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from __future__ import with_statement
22
from os import path
33
from flask import _request_ctx_stack, url_for
4+
from flask.templating import render_template_string
45
from webassets.env import (\
56
BaseEnvironment, ConfigStorage, env_options, Resolver, url_prefix_join)
7+
from webassets.filter import Filter, register_filter
68
from webassets.loaders import PythonLoader, YAMLLoader
79

810

@@ -15,7 +17,25 @@
1517

1618
# We want to expose this here.
1719
from webassets import Bundle
18-
from filter import Jinja2TemplateFilter
20+
21+
22+
class Jinja2Filter(Filter):
23+
"""Will compile all source files as Jinja2 temlates using the standard
24+
Flask contexts.
25+
"""
26+
name = 'jinja2'
27+
28+
def __init__(self, context=None):
29+
super(Jinja2Filter, self).__init__()
30+
self.context = context or {}
31+
32+
def input(self, _in, out, source_path, output_path, **kw):
33+
out.write(render_template_string(_in.read(), **self.context))
34+
35+
# Override the built-in ``jinja2`` filter that ships with ``webassets``. This
36+
# custom filter uses Flask's ``render_template_string`` function to provide all
37+
# the standard Flask template context variables.
38+
register_filter(Jinja2Filter)
1939

2040

2141
class FlaskConfigStorage(ConfigStorage):

0 commit comments

Comments
 (0)
0