Login

inclusion tag with template as variable

Author:
forgems
Posted:
February 19, 2009
Language:
Python
Version:
1.0
Score:
1 (after 1 ratings)

Sometimes you need to write a tag that renders other template, but the template name depends on template tag arguments. Usually you use simple_tag or write your own Node class. Here is a simple aproach that uses inclusion_tag. This way you can use context objects when used with takes_context=True

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
dummy_extends_template.html 
=========================
{% extends template %}


template_tag_code.py
===============================
from django.template import Template, Library

register = Library()

# you can use {% include template %} to

...

@register.inclusion_tag("dummy_extends_template.html" )
def my_template_tag(template, arg1, arg2, arg3):
    """ template can be Template object or template name """
    ... 
    return {'template':template, 'arg1':arg1, 'arg2':arg2 }

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

Please login first before commenting.