I sometimes access the context by supplying ‘takes_context=True’ for Django tags; Usually to acess the request.
But are there performance implications.
My mental model of how templates worke is that the tag function assembles the nodes representing the template, and that thereafter these nodes can render content without recreating the node, or reparsing the template.
But surely, if the tag function can be made to return different nodes, depending on
Something in the context, then the nodes will have to be recreated everytime the context is different (I.E every time).
Either that, or you are stuck with what nodes you get first time round, in which case you shouldn’t return nodes based on anything in the context (in which case, what’s the point?).
Can someone clear this up for me? I’ using Django 1.4.2.
Nodes are created when a template is loaded. When it’s rendered, context is simply passed to the Node’s render method. The only difference
simple_tag‘stakes_contextargument makes is making the resultingNode.rendercode pass context along to your function. So no, there are no performance implications to usingtakes_context.