Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #11989 -- Allow passing a Template instance to inclusion_tag. T…

…hanks to Jeremy Dunck and tcoenen.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16374 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0ebaf10258bf9fad4d540025a6eb3a8c471b284c 1 parent d27f909
Jannis Leidel authored June 11, 2011
4  django/template/base.py
@@ -987,7 +987,9 @@ def render(self, context):
987 987
 
988 988
                     if not getattr(self, 'nodelist', False):
989 989
                         from django.template.loader import get_template, select_template
990  
-                        if not isinstance(file_name, basestring) and is_iterable(file_name):
  990
+                        if isinstance(file_name, Template):
  991
+                            t = file_name
  992
+                        elif not isinstance(file_name, basestring) and is_iterable(file_name):
991 993
                             t = select_template(file_name)
992 994
                         else:
993 995
                             t = get_template(file_name)
9  docs/howto/custom-template-tags.txt
@@ -822,6 +822,15 @@ loader, we'd register the tag like this::
822 822
     # Here, register is a django.template.Library instance, as before
823 823
     register.inclusion_tag('results.html')(show_results)
824 824
 
  825
+.. versionchanged:: 1.4
  826
+
  827
+    Alternatively it is possible to register the inclusion tag using a
  828
+    :class:`django.template.Template` instance::
  829
+
  830
+        from django.template.loader import get_template
  831
+        t = get_template('results.html')
  832
+        register.inclusion_tag(t)(show_results)
  833
+
825 834
 As always, decorator syntax works as well, so we could have written::
826 835
 
827 836
     @register.inclusion_tag('results.html')
18  tests/regressiontests/templates/custom.py
@@ -73,6 +73,24 @@ def test_inclusion_tags(self):
73 73
         t = template.Template('{% load custom %}{% inclusion_params_and_context 37 %}')
74 74
         self.assertEqual(t.render(c), u'inclusion_params_and_context - Expected result (context value: 42): 37\n')
75 75
 
  76
+    def test_inclusion_tags_from_template(self):
  77
+        c = template.Context({'value': 42})
  78
+
  79
+        t = template.Template('{% load custom %}{% inclusion_no_params_from_template %}')
  80
+        self.assertEqual(t.render(c), u'inclusion_no_params_from_template - Expected result\n')
  81
+
  82
+        t = template.Template('{% load custom %}{% inclusion_one_param_from_template 37 %}')
  83
+        self.assertEqual(t.render(c), u'inclusion_one_param_from_template - Expected result: 37\n')
  84
+
  85
+        t = template.Template('{% load custom %}{% inclusion_explicit_no_context_from_template 37 %}')
  86
+        self.assertEqual(t.render(c), u'inclusion_explicit_no_context_from_template - Expected result: 37\n')
  87
+
  88
+        t = template.Template('{% load custom %}{% inclusion_no_params_with_context_from_template %}')
  89
+        self.assertEqual(t.render(c), u'inclusion_no_params_with_context_from_template - Expected result (context value: 42)\n')
  90
+
  91
+        t = template.Template('{% load custom %}{% inclusion_params_and_context_from_template 37 %}')
  92
+        self.assertEqual(t.render(c), u'inclusion_params_and_context_from_template - Expected result (context value: 42): 37\n')
  93
+
76 94
     def test_inclusion_tag_registration(self):
77 95
         # Test that the decorators preserve the decorated function's docstring, name and attributes.
78 96
         self.verify_tag(custom.inclusion_no_params, 'inclusion_no_params')
31  tests/regressiontests/templates/templatetags/custom.py
... ...
@@ -1,5 +1,6 @@
1 1
 from django import template
2 2
 from django.template.defaultfilters import stringfilter
  3
+from django.template.loader import get_template
3 4
 
4 5
 register = template.Library()
5 6
 
@@ -45,30 +46,60 @@ def inclusion_no_params():
45 46
     return {"result" : "inclusion_no_params - Expected result"}
46 47
 inclusion_no_params.anything = "Expected inclusion_no_params __dict__"
47 48
 
  49
+@register.inclusion_tag(get_template('inclusion.html'))
  50
+def inclusion_no_params_from_template():
  51
+    """Expected inclusion_no_params_from_template __doc__"""
  52
+    return {"result" : "inclusion_no_params_from_template - Expected result"}
  53
+inclusion_no_params_from_template.anything = "Expected inclusion_no_params_from_template __dict__"
  54
+
48 55
 @register.inclusion_tag('inclusion.html')
49 56
 def inclusion_one_param(arg):
50 57
     """Expected inclusion_one_param __doc__"""
51 58
     return {"result" : "inclusion_one_param - Expected result: %s" % arg}
52 59
 inclusion_one_param.anything = "Expected inclusion_one_param __dict__"
53 60
 
  61
+@register.inclusion_tag(get_template('inclusion.html'))
  62
+def inclusion_one_param_from_template(arg):
  63
+    """Expected inclusion_one_param_from_template __doc__"""
  64
+    return {"result" : "inclusion_one_param_from_template - Expected result: %s" % arg}
  65
+inclusion_one_param_from_template.anything = "Expected inclusion_one_param_from_template __dict__"
  66
+
54 67
 @register.inclusion_tag('inclusion.html', takes_context=False)
55 68
 def inclusion_explicit_no_context(arg):
56 69
     """Expected inclusion_explicit_no_context __doc__"""
57 70
     return {"result" : "inclusion_explicit_no_context - Expected result: %s" % arg}
58 71
 inclusion_explicit_no_context.anything = "Expected inclusion_explicit_no_context __dict__"
59 72
 
  73
+@register.inclusion_tag(get_template('inclusion.html'), takes_context=False)
  74
+def inclusion_explicit_no_context_from_template(arg):
  75
+    """Expected inclusion_explicit_no_context_from_template __doc__"""
  76
+    return {"result" : "inclusion_explicit_no_context_from_template - Expected result: %s" % arg}
  77
+inclusion_explicit_no_context_from_template.anything = "Expected inclusion_explicit_no_context_from_template __dict__"
  78
+
60 79
 @register.inclusion_tag('inclusion.html', takes_context=True)
61 80
 def inclusion_no_params_with_context(context):
62 81
     """Expected inclusion_no_params_with_context __doc__"""
63 82
     return {"result" : "inclusion_no_params_with_context - Expected result (context value: %s)" % context['value']}
64 83
 inclusion_no_params_with_context.anything = "Expected inclusion_no_params_with_context __dict__"
65 84
 
  85
+@register.inclusion_tag(get_template('inclusion.html'), takes_context=True)
  86
+def inclusion_no_params_with_context_from_template(context):
  87
+    """Expected inclusion_no_params_with_context_from_template __doc__"""
  88
+    return {"result" : "inclusion_no_params_with_context_from_template - Expected result (context value: %s)" % context['value']}
  89
+inclusion_no_params_with_context_from_template.anything = "Expected inclusion_no_params_with_context_from_template __dict__"
  90
+
66 91
 @register.inclusion_tag('inclusion.html', takes_context=True)
67 92
 def inclusion_params_and_context(context, arg):
68 93
     """Expected inclusion_params_and_context __doc__"""
69 94
     return {"result" : "inclusion_params_and_context - Expected result (context value: %s): %s" % (context['value'], arg)}
70 95
 inclusion_params_and_context.anything = "Expected inclusion_params_and_context __dict__"
71 96
 
  97
+@register.inclusion_tag(get_template('inclusion.html'), takes_context=True)
  98
+def inclusion_params_and_context_from_template(context, arg):
  99
+    """Expected inclusion_params_and_context_from_template __doc__"""
  100
+    return {"result" : "inclusion_params_and_context_from_template - Expected result (context value: %s): %s" % (context['value'], arg)}
  101
+inclusion_params_and_context_from_template.anything = "Expected inclusion_params_and_context_from_template __dict__"
  102
+
72 103
 @register.simple_tag(takes_context=True)
73 104
 def current_app(context):
74 105
     return "%s" % context.current_app

0 notes on commit 0ebaf10

Please sign in to comment.
Something went wrong with that request. Please try again.