librelist archives

« back to archive

get_template_attribute() with context

get_template_attribute() with context

From:
Jure Vrscaj
Date:
2011-09-06 @ 18:53
Hi flask list,

this is maybe more related to Jinja, but still:

Is there a way to get a macro obtained by get_template_attribute()
executed as if it was imported "with context" (import "macros.html" as
macros with context)? That way the macro function could access
request, session, etc...

This is the code I'm using:

@app.route('/render/<name>', methods=['GET', 'POST'])
def render(name):
    from flask import get_template_attribute
    m = get_template_attribute('render.html', name)
    # this has no access to request, session, ...
    result = m(**dict(request.values.iteritems()))
    return make_response(result)

Thanks,
Jure

Re: [flask] get_template_attribute() with context

From:
Armin Ronacher
Date:
2011-09-06 @ 22:00
Hi,

Yes, by using the lower level API:

t = app.jinja_env.get_template('template.html')
mod = t.make_module({'context': 'here'})
macro = mod.name_of_macro
rv = macro(the, arguments)


Regards,
Armin