Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@jezdez jezdez authored
View
4 django/template/base.py
@@ -987,7 +987,9 @@ def render(self, context):
if not getattr(self, 'nodelist', False):
from django.template.loader import get_template, select_template
- if not isinstance(file_name, basestring) and is_iterable(file_name):
+ if isinstance(file_name, Template):
+ t = file_name
+ elif not isinstance(file_name, basestring) and is_iterable(file_name):
t = select_template(file_name)
else:
t = get_template(file_name)
View
9 docs/howto/custom-template-tags.txt
@@ -822,6 +822,15 @@ loader, we'd register the tag like this::
# Here, register is a django.template.Library instance, as before
register.inclusion_tag('results.html')(show_results)
+.. versionchanged:: 1.4
+
+ Alternatively it is possible to register the inclusion tag using a
+ :class:`django.template.Template` instance::
+
+ from django.template.loader import get_template
+ t = get_template('results.html')
+ register.inclusion_tag(t)(show_results)
+
As always, decorator syntax works as well, so we could have written::
@register.inclusion_tag('results.html')
View
18 tests/regressiontests/templates/custom.py
@@ -73,6 +73,24 @@ def test_inclusion_tags(self):
t = template.Template('{% load custom %}{% inclusion_params_and_context 37 %}')
self.assertEqual(t.render(c), u'inclusion_params_and_context - Expected result (context value: 42): 37\n')
+ def test_inclusion_tags_from_template(self):
+ c = template.Context({'value': 42})
+
+ t = template.Template('{% load custom %}{% inclusion_no_params_from_template %}')
+ self.assertEqual(t.render(c), u'inclusion_no_params_from_template - Expected result\n')
+
+ t = template.Template('{% load custom %}{% inclusion_one_param_from_template 37 %}')
+ self.assertEqual(t.render(c), u'inclusion_one_param_from_template - Expected result: 37\n')
+
+ t = template.Template('{% load custom %}{% inclusion_explicit_no_context_from_template 37 %}')
+ self.assertEqual(t.render(c), u'inclusion_explicit_no_context_from_template - Expected result: 37\n')
+
+ t = template.Template('{% load custom %}{% inclusion_no_params_with_context_from_template %}')
+ self.assertEqual(t.render(c), u'inclusion_no_params_with_context_from_template - Expected result (context value: 42)\n')
+
+ t = template.Template('{% load custom %}{% inclusion_params_and_context_from_template 37 %}')
+ self.assertEqual(t.render(c), u'inclusion_params_and_context_from_template - Expected result (context value: 42): 37\n')
+
def test_inclusion_tag_registration(self):
# Test that the decorators preserve the decorated function's docstring, name and attributes.
self.verify_tag(custom.inclusion_no_params, 'inclusion_no_params')
View
31 tests/regressiontests/templates/templatetags/custom.py
@@ -1,5 +1,6 @@
from django import template
from django.template.defaultfilters import stringfilter
+from django.template.loader import get_template
register = template.Library()
@@ -45,30 +46,60 @@ def inclusion_no_params():
return {"result" : "inclusion_no_params - Expected result"}
inclusion_no_params.anything = "Expected inclusion_no_params __dict__"
+@register.inclusion_tag(get_template('inclusion.html'))
+def inclusion_no_params_from_template():
+ """Expected inclusion_no_params_from_template __doc__"""
+ return {"result" : "inclusion_no_params_from_template - Expected result"}
+inclusion_no_params_from_template.anything = "Expected inclusion_no_params_from_template __dict__"
+
@register.inclusion_tag('inclusion.html')
def inclusion_one_param(arg):
"""Expected inclusion_one_param __doc__"""
return {"result" : "inclusion_one_param - Expected result: %s" % arg}
inclusion_one_param.anything = "Expected inclusion_one_param __dict__"
+@register.inclusion_tag(get_template('inclusion.html'))
+def inclusion_one_param_from_template(arg):
+ """Expected inclusion_one_param_from_template __doc__"""
+ return {"result" : "inclusion_one_param_from_template - Expected result: %s" % arg}
+inclusion_one_param_from_template.anything = "Expected inclusion_one_param_from_template __dict__"
+
@register.inclusion_tag('inclusion.html', takes_context=False)
def inclusion_explicit_no_context(arg):
"""Expected inclusion_explicit_no_context __doc__"""
return {"result" : "inclusion_explicit_no_context - Expected result: %s" % arg}
inclusion_explicit_no_context.anything = "Expected inclusion_explicit_no_context __dict__"
+@register.inclusion_tag(get_template('inclusion.html'), takes_context=False)
+def inclusion_explicit_no_context_from_template(arg):
+ """Expected inclusion_explicit_no_context_from_template __doc__"""
+ return {"result" : "inclusion_explicit_no_context_from_template - Expected result: %s" % arg}
+inclusion_explicit_no_context_from_template.anything = "Expected inclusion_explicit_no_context_from_template __dict__"
+
@register.inclusion_tag('inclusion.html', takes_context=True)
def inclusion_no_params_with_context(context):
"""Expected inclusion_no_params_with_context __doc__"""
return {"result" : "inclusion_no_params_with_context - Expected result (context value: %s)" % context['value']}
inclusion_no_params_with_context.anything = "Expected inclusion_no_params_with_context __dict__"
+@register.inclusion_tag(get_template('inclusion.html'), takes_context=True)
+def inclusion_no_params_with_context_from_template(context):
+ """Expected inclusion_no_params_with_context_from_template __doc__"""
+ return {"result" : "inclusion_no_params_with_context_from_template - Expected result (context value: %s)" % context['value']}
+inclusion_no_params_with_context_from_template.anything = "Expected inclusion_no_params_with_context_from_template __dict__"
+
@register.inclusion_tag('inclusion.html', takes_context=True)
def inclusion_params_and_context(context, arg):
"""Expected inclusion_params_and_context __doc__"""
return {"result" : "inclusion_params_and_context - Expected result (context value: %s): %s" % (context['value'], arg)}
inclusion_params_and_context.anything = "Expected inclusion_params_and_context __dict__"
+@register.inclusion_tag(get_template('inclusion.html'), takes_context=True)
+def inclusion_params_and_context_from_template(context, arg):
+ """Expected inclusion_params_and_context_from_template __doc__"""
+ return {"result" : "inclusion_params_and_context_from_template - Expected result (context value: %s): %s" % (context['value'], arg)}
+inclusion_params_and_context_from_template.anything = "Expected inclusion_params_and_context_from_template __dict__"
+
@register.simple_tag(takes_context=True)
def current_app(context):
return "%s" % context.current_app
Please sign in to comment.
Something went wrong with that request. Please try again.