Ticket #11989: inclusion_template2.diff

File inclusion_template2.diff, 7.4 KB (added by tcoenen, 13 years ago)

Updated patch now includes tests and documentation as well.

  • docs/howto/custom-template-tags.txt

     
    812812    # Here, register is a django.template.Library instance, as before
    813813    register.inclusion_tag('results.html')(show_results)
    814814
     815Alternatively 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
    815822As always, decorator syntax works as well, so we could have written::
    816823
    817824    @register.inclusion_tag('results.html')
  • django/template/base.py

     
    985985
    986986                    if not getattr(self, 'nodelist', False):
    987987                        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):
    989991                            t = select_template(file_name)
    990992                        else:
    991993                            t = get_template(file_name)
  • tests/regressiontests/templates/templatetags/custom.py

     
    11from django import template
    22from django.template.defaultfilters import stringfilter
     3from django.template.loader import get_template
    34
    45register = template.Library()
    56
     
    4546    return {"result" : "inclusion_no_params - Expected result"}
    4647inclusion_no_params.anything = "Expected inclusion_no_params __dict__"
    4748
     49@register.inclusion_tag(get_template('inclusion.html'))
     50def inclusion_no_params_from_template():
     51    """Expected inclusion_no_params_from_template __doc__"""
     52    return {"result" : "inclusion_no_params_from_template - Expected result"}
     53inclusion_no_params_from_template.anything = "Expected inclusion_no_params_from_template __dict__"
     54
    4855@register.inclusion_tag('inclusion.html')
    4956def inclusion_one_param(arg):
    5057    """Expected inclusion_one_param __doc__"""
    5158    return {"result" : "inclusion_one_param - Expected result: %s" % arg}
    5259inclusion_one_param.anything = "Expected inclusion_one_param __dict__"
    5360
     61@register.inclusion_tag(get_template('inclusion.html'))
     62def 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}
     65inclusion_one_param_from_template.anything = "Expected inclusion_one_param_from_template __dict__"
     66
    5467@register.inclusion_tag('inclusion.html', takes_context=False)
    5568def inclusion_explicit_no_context(arg):
    5669    """Expected inclusion_explicit_no_context __doc__"""
    5770    return {"result" : "inclusion_explicit_no_context - Expected result: %s" % arg}
    5871inclusion_explicit_no_context.anything = "Expected inclusion_explicit_no_context __dict__"
    5972
     73@register.inclusion_tag(get_template('inclusion.html'), takes_context=False)
     74def 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}
     77inclusion_explicit_no_context_from_template.anything = "Expected inclusion_explicit_no_context_from_template __dict__"
     78
    6079@register.inclusion_tag('inclusion.html', takes_context=True)
    6180def inclusion_no_params_with_context(context):
    6281    """Expected inclusion_no_params_with_context __doc__"""
    6382    return {"result" : "inclusion_no_params_with_context - Expected result (context value: %s)" % context['value']}
    6483inclusion_no_params_with_context.anything = "Expected inclusion_no_params_with_context __dict__"
    6584
     85@register.inclusion_tag(get_template('inclusion.html'), takes_context=True)
     86def 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']}
     89inclusion_no_params_with_context_from_template.anything = "Expected inclusion_no_params_with_context_from_template __dict__"
     90
    6691@register.inclusion_tag('inclusion.html', takes_context=True)
    6792def inclusion_params_and_context(context, arg):
    6893    """Expected inclusion_params_and_context __doc__"""
    6994    return {"result" : "inclusion_params_and_context - Expected result (context value: %s): %s" % (context['value'], arg)}
    7095inclusion_params_and_context.anything = "Expected inclusion_params_and_context __dict__"
    7196
     97@register.inclusion_tag(get_template('inclusion.html'), takes_context=True)
     98def 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)}
     101inclusion_params_and_context_from_template.anything = "Expected inclusion_params_and_context_from_template __dict__"
     102
    72103@register.simple_tag(takes_context=True)
    73104def current_app(context):
    74105    return "%s" % context.current_app
  • tests/regressiontests/templates/custom.py

     
    7373        t = template.Template('{% load custom %}{% inclusion_params_and_context 37 %}')
    7474        self.assertEqual(t.render(c), u'inclusion_params_and_context - Expected result (context value: 42): 37\n')
    7575
     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
    7694    def test_inclusion_tag_registration(self):
    7795        # Test that the decorators preserve the decorated function's docstring, name and attributes.
    7896        self.verify_tag(custom.inclusion_no_params, 'inclusion_no_params')
Back to Top