|
1 | 1 | from __future__ import with_statement
|
2 | 2 | from os import path
|
3 | 3 | from flask import _request_ctx_stack, url_for
|
| 4 | +from flask.templating import render_template_string |
4 | 5 | from webassets.env import (\
|
5 | 6 | BaseEnvironment, ConfigStorage, env_options, Resolver, url_prefix_join)
|
| 7 | +from webassets.filter import Filter, register_filter |
6 | 8 | from webassets.loaders import PythonLoader, YAMLLoader
|
7 | 9 |
|
8 | 10 |
|
|
15 | 17 |
|
16 | 18 | # We want to expose this here.
|
17 | 19 | 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) |
19 | 39 |
|
20 | 40 |
|
21 | 41 | class FlaskConfigStorage(ConfigStorage):
|
|
0 commit comments