built-in dependency management across macros #31

Open
jab opened this Issue May 5, 2011 · 2 comments

Projects

None yet

2 participants

@jab
Contributor
jab commented May 5, 2011

Some macros may depend on other macros. Currently if you'd like to manage dependencies across macros, you have to roll your own (a la http://paste.pocoo.org/show/379402/ ).

@jab
Contributor
jab commented May 6, 2011

Here's an example from a codebase that's actually in use: http://paste.pocoo.org/show/489070/
(This was a first pass, the implementation has since improved a little.)

Fortunately I had the luxury of being able to move the dump() calls to the end of my base template because otherwise the register() calls were happening after (so dump() would output nothing). But having to worry about the order of these calls is a flaw of this solution.

Perhaps a built-in facility for this could guarantee the dump calls being evaluated last.

@danofames

we're interested in something very similar, has any more thought been given toward how this could happen?

the end result I'd look for is a base template that looks like:

{% import 'dateselector.tpl' as dateselector %}
<html>
    <head>
        <link href="base.css">
        {{ some_extension_function_to_write_out_macro_set_links() }}
        {# or #}
        {% for link in some_extension_function_that_yields_macro_set_links() %}
        <link href="{{ link }}">
        {% endfor %}
    </head>
    <body>
    content
    {{ dateselector.single() }}
    </body>
</html>

and dateselector.tpl is:

{% macro single() %}
{% some_extension_function_to_add_specified_link_to_head 'dateselector.css' %}
<input type="text" name="thisdate" />
{% endmacro %}

a few questions as I'm getting started looking at how to do this in more depth:

  • does the template parsing and rendering procedure allow for this approach?
  • it's not clear how one would add a set style tag like the some_extension_function_to_add_specified_link_to_head above. there seems to be support for custom tags that have the endtag and the block body, but not a custom tag in that manner.
  • if those two questions are resolved, where would the list of files used in some_extension_function_to_write_out_macro_set_links and built through some_extension_function_to_add_specified_link_to_head be stored. there are some examples that set a variable on environment, but that doesn't seem quite right.

this proposal on stack exchange achieves this in a way:
http://stackoverflow.com/questions/4292630/insert-javascript-at-top-of-including-file-in-jinja-2

but I'm not sure about the use of self.environment.globals[name] to store the output (just a hunch, that may actually be recommended). and I'd rather store a list of files that needed to be added as links (so a function could remove duplicates, for example).

I guess what I'm saying is, we'd love to put in some work on writing such an extension, and a nudge in the right direction would be helpful.

thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment