Ticket #12212: 12212.patch

File 12212.patch, 5.6 KB (added by Leo Shklovskii, 8 years ago)

Patch against r12699

  • django/template/__init__.py

     
    929929                self.vars_to_resolve = map(Variable, vars_to_resolve)
    930930
    931931            def render(self, context):
    932                 resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
     932                resolved_vars = []
     933                for var in self.vars_to_resolve:
     934                    try:
     935                        resolved_vars.append(var.resolve(context))
     936                    except VariableDoesNotExist:
     937                        resolved_vars.append(None)
     938               
    933939                return func(*resolved_vars)
    934940
    935941        compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode)
     
    951957                    self.vars_to_resolve = map(Variable, vars_to_resolve)
    952958
    953959                def render(self, context):
    954                     resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
     960                    resolved_vars = []
     961                    for var in self.vars_to_resolve:
     962                        try:
     963                            resolved_vars.append(var.resolve(context))
     964                        except VariableDoesNotExist:
     965                            resolved_vars.append(None)
     966                   
    955967                    if takes_context:
    956968                        args = [context] + resolved_vars
    957969                    else:
  • docs/howto/custom-template-tags.txt

     
    669669      so we just receive a plain string.
    670670    * If the argument was a template variable, our function is passed the
    671671      current value of the variable, not the variable itself.
     672    * If a variable with the argument's name does not exist in the context the
     673      argument will be set to ``None``.
    672674
    673675When your template tag does not need access to the current context, writing a
    674676function to work with the input values and using the ``simple_tag`` helper is
     
    788790the tag is passed the context object, as in this example. That's the only
    789791difference between this case and the previous ``inclusion_tag`` example.
    790792
     793.. note::
     794    Named arguments in the function wrapped by ``inclusion_tag`` will be set
     795    to ``None`` for variables that don't exist in the context. If
     796    ``takes_context=True`` they will not exist in the ``context`` dictionary
     797    either.
     798
     799
    791800Setting a variable in the context
    792801~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    793802
  • tests/regressiontests/templates/taghelpers.py

     
     1import unittest
     2from django.template import Template, Context
     3
     4class InclusionTagTests(unittest.TestCase):
     5    def test_variable_not_found(self):
     6        t = Template("{% load custom %}Testing - {% it_sample dog %}")
     7        self.assertEqual(t.render(Context({})), u'Testing - None')
     8
     9    def test_variable_not_found_with_takes_context(self):
     10        t = Template("{% load custom %}Testing - {% it_takes_context %}")
     11        self.assertRaises(KeyError, t.render, Context({}))
     12
     13    def test_variable_not_found_with_takes_context_arg(self):
     14        t = Template("{% load custom %}Testing - {% it_takes_context_arg dog %}")
     15        self.assertEqual(t.render(Context({})), u'Testing - None')
     16       
     17class SimpleTagTests(unittest.TestCase):
     18    def test_variable_not_found(self):
     19        t = Template("{% load custom %}Testing - {% st_sample dog %}")
     20        self.assertEqual(t.render(Context({})), u'Testing - None')
  • tests/regressiontests/templates/templatetags/custom.py

     
    99
    1010register.filter(trim)
    1111
     12def it_sample(foo):
     13    return {'bar': foo}
     14
     15register.inclusion_tag('taghelpers_test.txt')(it_sample)
     16
     17def it_takes_context(context):
     18    return {'bar': context['foo']}
     19
     20register.inclusion_tag('taghelpers_test.txt', takes_context=True)(it_takes_context)
     21
     22def it_takes_context_arg(context, foo):
     23    return {'bar': foo}
     24
     25register.inclusion_tag('taghelpers_test.txt', takes_context=True)(it_takes_context_arg)
     26
     27def st_sample(foo):
     28    return foo
     29
     30register.simple_tag(st_sample)
     31 No newline at end of file
  • tests/regressiontests/templates/tests.py

     
    2525from parser import token_parsing, filter_parsing, variable_parsing
    2626from unicode import unicode_tests
    2727from nodelist import NodelistTest
    28 from smartif import *
     28from smartif import SmartIfTests
     29from taghelpers import InclusionTagTests, SimpleTagTests
    2930
    3031try:
    3132    from loaders import *
  • tests/templates/taghelpers_test.txt

     
     1{{ bar }}
     2 No newline at end of file
Back to Top