Django templatetag "order of processing"

Posted by Jason Persampieri on Stack Overflow See other posts from Stack Overflow or by Jason Persampieri
Published on 2010-05-25T23:57:10Z Indexed on 2010/05/26 0:01 UTC
Read the original article Hit count: 195

I am trying to write a set of template tags that allow you to easily specify js and css files from within the template files themselves. Something along the lines of {% requires global.css %}, and later in the request, {% get_required_css %}.

I have this mostly working, but there are a couple of issues. We'll start with the 'timing' issues.

Each template tag is made up of two steps, call/init and render. Every call/init happens before any render procedure is called. In order to guarantee that all of the files are queued before the {% get_required_css %} is rendered, I need to build my list of required files in the call/init procedures themselves.

So, I need to collect all of the files into one bundle per request. The context dict is obviously the place for this, but unfortunately, the call/init doesn't have access to the context variable.

Is this making sense? Anyone see a way around this (without resorting to a hack-y global request object)?

Another possibility to store these in a local dict but they would still need to be tied to the request somehow... possibly some sort of {% start_requires %} tag? But I have no clue how to make that work either.

© Stack Overflow or respective owner

Related posts about django

Related posts about django-template-tags