Ticket #11989: inclusion_template2.diff
File inclusion_template2.diff, 7.4 KB (added by , 13 years ago) |
---|
-
docs/howto/custom-template-tags.txt
812 812 # Here, register is a django.template.Library instance, as before 813 813 register.inclusion_tag('results.html')(show_results) 814 814 815 Alternatively it is possible to register the inclusion tag using a 816 ``django.template.Template`` instance:: 817 818 from django.template.loader import get_template 819 t = get_template('results.html') 820 register.inclusion_tag(t)(show_results) 821 815 822 As always, decorator syntax works as well, so we could have written:: 816 823 817 824 @register.inclusion_tag('results.html') -
django/template/base.py
985 985 986 986 if not getattr(self, 'nodelist', False): 987 987 from django.template.loader import get_template, select_template 988 if not isinstance(file_name, basestring) and is_iterable(file_name): 988 if isinstance(file_name, Template): 989 t = file_name 990 elif not isinstance(file_name, basestring) and is_iterable(file_name): 989 991 t = select_template(file_name) 990 992 else: 991 993 t = get_template(file_name) -
tests/regressiontests/templates/templatetags/custom.py
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 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 -
tests/regressiontests/templates/custom.py
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')