Ticket #12212: 12212.patch
File 12212.patch, 5.6 KB (added by , 15 years ago) |
---|
-
django/template/__init__.py
929 929 self.vars_to_resolve = map(Variable, vars_to_resolve) 930 930 931 931 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 933 939 return func(*resolved_vars) 934 940 935 941 compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode) … … 951 957 self.vars_to_resolve = map(Variable, vars_to_resolve) 952 958 953 959 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 955 967 if takes_context: 956 968 args = [context] + resolved_vars 957 969 else: -
docs/howto/custom-template-tags.txt
669 669 so we just receive a plain string. 670 670 * If the argument was a template variable, our function is passed the 671 671 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``. 672 674 673 675 When your template tag does not need access to the current context, writing a 674 676 function to work with the input values and using the ``simple_tag`` helper is … … 788 790 the tag is passed the context object, as in this example. That's the only 789 791 difference between this case and the previous ``inclusion_tag`` example. 790 792 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 791 800 Setting a variable in the context 792 801 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 793 802 -
tests/regressiontests/templates/taghelpers.py
1 import unittest 2 from django.template import Template, Context 3 4 class 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 17 class 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
9 9 10 10 register.filter(trim) 11 11 12 def it_sample(foo): 13 return {'bar': foo} 14 15 register.inclusion_tag('taghelpers_test.txt')(it_sample) 16 17 def it_takes_context(context): 18 return {'bar': context['foo']} 19 20 register.inclusion_tag('taghelpers_test.txt', takes_context=True)(it_takes_context) 21 22 def it_takes_context_arg(context, foo): 23 return {'bar': foo} 24 25 register.inclusion_tag('taghelpers_test.txt', takes_context=True)(it_takes_context_arg) 26 27 def st_sample(foo): 28 return foo 29 30 register.simple_tag(st_sample) 31 No newline at end of file -
tests/regressiontests/templates/tests.py
25 25 from parser import token_parsing, filter_parsing, variable_parsing 26 26 from unicode import unicode_tests 27 27 from nodelist import NodelistTest 28 from smartif import * 28 from smartif import SmartIfTests 29 from taghelpers import InclusionTagTests, SimpleTagTests 29 30 30 31 try: 31 32 from loaders import * -
tests/templates/taghelpers_test.txt
1 {{ bar }} 2 No newline at end of file