librelist archives

« back to archive

Loading a jinja2 extension in to Flask...

Loading a jinja2 extension in to Flask...

From:
Sean Chittenden
Date:
2011-09-25 @ 21:51
Someone pointed out the jinja2 HTMLCompress module[1] on IRC, so naturally
I had to try it out. :~]

Is the following snippet the "correct" way to set a jinja2_option? My 
concern comes from the fact that jinja_options is an ImmutableDict and 
it's not clear to me that appending to a value is supposed to work or is 
permitted (or will break down the road). Thanks in advance. -sc

> def create_app(name = __name__):
>     app = FlaskWithHTMLCompress(name, static_path='/static')
>     app.jinja_options['extensions'].append('jinja2htmlcompress.HTMLCompress')
>     ...

[1] https://github.com/mitsuhiko/jinja2-htmlcompress

--
Sean Chittenden
sean@chittenden.org

Re: [flask] Loading a jinja2 extension in to Flask...

From:
DasIch
Date:
2011-09-25 @ 22:00
jinja_options is passed to the Environment on its creation. In order
to use it you have to subclass like this:

    class MyFlask(Flask):
        jinja_options = dict(Flask.jinja_options)
        jinja_options.setdefault('extensions',
[]).append('jinja2htmlcompress.HTMLCompress')

Re: [flask] Loading a jinja2 extension in to Flask...

From:
Jonathan Zempel
Date:
2011-09-25 @ 21:55
This should work:

app.jinja_env.add_extension("jinja2htmlcompress.HTMLCompress")

On Sun, Sep 25, 2011 at 2:51 PM, Sean Chittenden <sean@chittenden.org>wrote:

> Someone pointed out the jinja2 HTMLCompress module[1] on IRC, so naturally
> I had to try it out. :~]
>
> Is the following snippet the "correct" way to set a jinja2_option? My
> concern comes from the fact that jinja_options is an ImmutableDict and it's
> not clear to me that appending to a value is supposed to work or is
> permitted (or will break down the road). Thanks in advance. -sc
>
> > def create_app(name = __name__):
> >     app = FlaskWithHTMLCompress(name, static_path='/static')
> >
> app.jinja_options['extensions'].append('jinja2htmlcompress.HTMLCompress')
> >     ...
>
> [1] https://github.com/mitsuhiko/jinja2-htmlcompress
>
> --
> Sean Chittenden
> sean@chittenden.org
>
>

Re: [flask] Loading a jinja2 extension in to Flask...

From:
Sean Chittenden
Date:
2011-09-25 @ 22:02
> This should work:
> 
> app.jinja_env.add_extension("jinja2htmlcompress.HTMLCompress")

Thanks, that's  much better API.... and undocumented. Much appreciated! -sc


> On Sun, Sep 25, 2011 at 2:51 PM, Sean Chittenden <sean@chittenden.org> wrote:
> Someone pointed out the jinja2 HTMLCompress module[1] on IRC, so 
naturally I had to try it out. :~]
> 
> Is the following snippet the "correct" way to set a jinja2_option? My 
concern comes from the fact that jinja_options is an ImmutableDict and 
it's not clear to me that appending to a value is supposed to work or is 
permitted (or will break down the road). Thanks in advance. -sc
> 
> > def create_app(name = __name__):
> >     app = FlaskWithHTMLCompress(name, static_path='/static')
> >     app.jinja_options['extensions'].append('jinja2htmlcompress.HTMLCompress')
> >     ...
> 
> [1] https://github.com/mitsuhiko/jinja2-htmlcompress


--
Sean Chittenden
sean@chittenden.org

Re: [flask] Loading a jinja2 extension in to Flask...

From:
Armin Ronacher
Date:
2011-09-25 @ 22:42
Hi,

On 2011-09-26 12:02 AM, Sean Chittenden wrote:
> Thanks, that's much better API.... and undocumented. Much appreciated! -sc
It was omitted by accident from the list of public methods.  I fixed 
that and uploaded an updated documentation.

Generally you can also modify the jinja options but you need to do that 
before the environment is created and you need to make a copy of it:

app = Flask(__name__)
app.jinja_options = app.jinja_options.copy()
app.jinja_options['extensions'].extend(...)


Regards,
Armin