Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

built-in dependency management across macros #31

Closed
jab opened this issue May 5, 2011 · 3 comments
Closed

built-in dependency management across macros #31

jab opened this issue May 5, 2011 · 3 comments

Comments

@jab
Copy link
Member

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
Copy link
Member Author

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
Copy link

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.

@davidism
Copy link
Member

Closing as I'm not exactly sure what's being asked for anymore, and the complexity does not seem like a good fit in Jinja right now.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants