Ticket #7462: simple_tag_takes_context.diff
File simple_tag_takes_context.diff, 7.2 KB (added by , 16 years ago) |
---|
-
E:/Software/workspace/django/django/template/__init__.py
49 49 u'<html></html>' 50 50 """ 51 51 import re 52 from inspect import getargspec 52 from inspect import getargspec, isfunction 53 53 from django.conf import settings 54 54 from django.template.context import Context, RequestContext, ContextPopException 55 55 from django.utils.itercompat import is_iterable … … 859 859 self.filters[getattr(func, "_decorated_function", func).__name__] = func 860 860 return func 861 861 862 def simple_tag(self,func): 863 params, xx, xxx, defaults = getargspec(func) 862 def simple_tag(self, *args, **kwargs): 863 def dec(func): 864 params, xx, xxx, defaults = getargspec(func) 865 if takes_context: 866 if params[0] == 'context': 867 params = params[1:] 868 else: 869 raise TemplateSyntaxError("Any tag function decorated with takes_context=True must have a first argument of 'context'") 864 870 865 class SimpleNode(Node): 866 def __init__(self, vars_to_resolve): 867 self.vars_to_resolve = map(Variable, vars_to_resolve) 871 class SimpleNode(Node): 872 def __init__(self, vars_to_resolve): 873 self.vars_to_resolve = map(Variable, vars_to_resolve) 874 875 def render(self, context): 876 resolved_vars = [var.resolve(context) for var in self.vars_to_resolve] 877 if takes_context: 878 func_args = [context] + resolved_vars 879 else: 880 func_args = resolved_vars 881 return func(*func_args) 868 882 869 def render(self, context): 870 resolved_vars = [var.resolve(context) for var in self.vars_to_resolve] 871 return func(*resolved_vars) 883 compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode) 884 compile_func.__doc__ = func.__doc__ 885 self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) 886 return func 887 888 if len(args) == 1 and len(kwargs) == 0 and isfunction(args[0]): 889 takes_context = False 890 return dec(args[0]) 891 elif len(args) == 0 and len(kwargs) == 0: 892 takes_context = False 893 return dec 894 elif len(args) == 0 and len(kwargs) == 1 and 'takes_context' in kwargs: 895 takes_context = kwargs['takes_context'] 896 return dec 897 else: 898 raise TemplateSyntaxError("Incorrect parameters for simple_tag") 872 899 873 compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode)874 compile_func.__doc__ = func.__doc__875 self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)876 return func877 878 900 def inclusion_tag(self, file_name, context_class=Context, takes_context=False): 879 901 def dec(func): 880 902 params, xx, xxx, defaults = getargspec(func) -
E:/Software/workspace/django/tests/regressiontests/templates/tests.py
17 17 from django.utils.safestring import mark_safe 18 18 from django.utils.tzinfo import LocalTimezone 19 19 20 from decorators import DecoratorsTest 20 21 from unicode import unicode_tests 21 22 from context import context_tests 22 23 -
E:/Software/workspace/django/tests/regressiontests/templates/decorators.py
1 from unittest import TestCase 2 from sys import version_info 3 4 from django import template 5 6 register = template.Library() 7 8 def a_simple_tag(arg): 9 """Expected __doc__""" 10 return "Expected result" 11 a_simple_tag.anything = "Expected __dict__" 12 13 a_simple_tag = register.simple_tag(a_simple_tag) 14 15 16 def a_simple_tag_with_context(context, arg): 17 """Expected __doc__""" 18 return "Expected result" 19 a_simple_tag_with_context.anything = "Expected __dict__" 20 21 a_simple_tag_with_context = register.simple_tag(takes_context=True)(a_simple_tag_with_context) 22 23 24 25 class DecoratorsTest(TestCase): 26 27 def test_simple_tag(self): 28 # Only check __name__ on Python 2.4 or later since __name__ can't be 29 # assigned to in earlier Python versions. 30 if version_info[0] >= 2 and version_info[1] >= 4: 31 self.assertEquals(a_simple_tag.__name__, 'a_simple_tag') 32 self.assertEquals(a_simple_tag.__doc__, 'Expected __doc__') 33 self.assertEquals(a_simple_tag.__dict__['anything'], 'Expected __dict__') 34 35 36 def test_simple_tag_with_context(self): 37 # Only check __name__ on Python 2.4 or later since __name__ can't be 38 # assigned to in earlier Python versions. 39 if version_info[0] >= 2 and version_info[1] >= 4: 40 self.assertEquals(a_simple_tag_with_context.__name__, 'a_simple_tag_with_context') 41 self.assertEquals(a_simple_tag_with_context.__doc__, 'Expected __doc__') 42 self.assertEquals(a_simple_tag_with_context.__dict__['anything'], 'Expected __dict__') 43 No newline at end of file -
E:/Software/workspace/django/docs/templates_python.txt
1169 1169 In Python 2.4, the decorator syntax also works:: 1170 1170 1171 1171 @register.simple_tag 1172 def current_time( token):1172 def current_time(format_string): 1173 1173 ... 1174 1174 1175 1175 A couple of things to note about the ``simple_tag`` helper function: … … 1184 1184 function to work with the input values and using the ``simple_tag`` helper is 1185 1185 the easiest way to create a new tag. 1186 1186 1187 If your template tag *does* need to access the current context, you can use the 1188 ``takes_context`` option as follows:: 1189 1190 # The first argument *must* be called "context" here. 1191 def current_time(context, format_string): 1192 timezone = context['timezone'] 1193 ... 1194 1195 register.simple_tag(takes_context=True)(current_time) 1196 1197 You can also use the decorator syntax if running in Python 2.4:: 1198 1199 @register.simple_tag(takes_context=True) 1200 def current_time(context, format_string): 1201 1202 For more information on how the ``takes_context`` option works, see the section 1203 on `inclusion tags`_. 1204 1205 .. _inclusion tags: #inclusion-tags 1206 1187 1207 Inclusion tags 1188 1208 ~~~~~~~~~~~~~~ 1189 1209