From 0ebaf10258bf9fad4d540025a6eb3a8c471b284c Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Sat, 11 Jun 2011 16:22:45 +0000 Subject: [PATCH] Fixed #11989 -- Allow passing a Template instance to inclusion_tag. Thanks to Jeremy Dunck and tcoenen. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16374 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/base.py | 4 ++- docs/howto/custom-template-tags.txt | 9 ++++++ tests/regressiontests/templates/custom.py | 18 +++++++++++ .../templates/templatetags/custom.py | 31 +++++++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/django/template/base.py b/django/template/base.py index 78da99813b7..b96e446404a 100644 --- a/django/template/base.py +++ b/django/template/base.py @@ -987,7 +987,9 @@ class Library(object): 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) diff --git a/docs/howto/custom-template-tags.txt b/docs/howto/custom-template-tags.txt index 97f1e9a53b0..1e226840d63 100644 --- a/docs/howto/custom-template-tags.txt +++ b/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') diff --git a/tests/regressiontests/templates/custom.py b/tests/regressiontests/templates/custom.py index ec3e9dc1793..74c39ab7e00 100644 --- a/tests/regressiontests/templates/custom.py +++ b/tests/regressiontests/templates/custom.py @@ -73,6 +73,24 @@ class CustomTagTests(TestCase): 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') diff --git a/tests/regressiontests/templates/templatetags/custom.py b/tests/regressiontests/templates/templatetags/custom.py index 97f26749533..dfa4171eb09 100644 --- a/tests/regressiontests/templates/templatetags/custom.py +++ b/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