Hello,

This question is inspired by the navigation tips and trick item [0].

I am wondering if it is possible to create a Jinja2 function that
would append to a global list. Specifically I want to create functions
that look like the following:

addStyle('/path/to/style.css')
addScript('/path/to/script.js')

The idea is that each template in the inheritance hierarchy can add
their own styles and scripts to the page, and all inherited pages will
pull those files in in the proper order. I would expect to call the
above functions outside of any block, just like "{% set active_page =
"index" %}" in the linked example.

In the base.html template there will be something like the following:

{% for style in styles %}
<link type="text/css" href="{{ style }}" />
{% endfor %}

{% for script in scripts %}
<script type="text/javascript" src="{{ script }}"></script>
{% endfor %}

Currently I am getting similar behaviour by using blocks and super.
However, I find my above idea a little more elegant. However, I am
unsure how to create a function that will modify a global variable of
the current template evaluation. Also, would the template inheritance
evaluation order guarantee that these files are appending in the
correct order?

Thanks for any help,
Jon

[0] http://jinja.pocoo.org/tricks/#highlighting-active-menu-items

-- 
You received this message because you are subscribed to the Google Groups 
"pocoo-libs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pocoo-libs?hl=en.

Reply via email to