Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - replacing built-in templatetag by custom tag for a whole site without {% load .. %}

I want to replace the standard {% if %} template tag with a "smart if" custom tag from this snippet, because I don't want to write {% load smart_if %} every time. Also, "smart if" will come into core template system very soon.

I forgot where I saw a piece of code that does this. Does anyone know how to replace a built-in templatetag?

like image 419
ramusus Avatar asked Mar 01 '23 03:03

ramusus


1 Answers

Place this somewhere you know will get run:

from django.template import add_to_builtins
add_to_builtins('mysite.myapp.templatetags.smart_if')

... while placing smart_if.py containing the smart_if code at the appropriate location. This effectively overrides the if tag with "smart if" accross the whole site.

like image 156
Gabriel Ross Avatar answered May 03 '23 12:05

Gabriel Ross