mirror of https://github.com/django/django.git
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
This commit is contained in:
parent
d27f909d2e
commit
0ebaf10258
|
@ -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)
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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')
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue