Rahul
Rahul

Reputation: 1505

What is the scope of load tag in Django templates?

What is the scope of custom tags loaded using load in django templating system especially w.r.t Template Inheritance?

How can you effectively load the same custom tag in a tree of template hierarchy. I couldn't find either of the info in docs. Any pointers would be useful.

Thanks.

Upvotes: 4

Views: 777

Answers (1)

rubyruy
rubyruy

Reputation: 7932

I also wondered about this and found the documentation lacking, so I decided to do some testing on my own.

As of django 1.4, the answer is: the scope of a loaded custom tag is limited strictly to the file it is loaded in (using {% load %}) and absolutely nothing else.

The following attempts to use a custom tag have failed:

  • Template A extends Template B:
    • Load in A and attempt to use in B
    • Load in A and attempt to use in B inside a block
    • Load in a block in A and attempt to use in same block in B, with or without super
  • Template A includes Template B:
    • Load in A and attempt use in B
    • Load in A and attempt to pass into B explicitly using with context
  • Template A including Template B using `{% ssi ... parsed %}
    • Load in A and attempt to use in B

So yeah, it is strictly a per-file thing (which is good because it makes it impossible to squirrel in mystery tags from a distance - unless of course you do so on purpose by messing with the global loader (which you shouldn't))

Upvotes: 7

Related Questions