Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12847 -- Added name parameter to simple_tag, assignment_tag an…

…d inclusion_tag helpers. Thanks, vladmos.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16373 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d27f909d2e8e215dc9c52d090cee393a719a7965 1 parent 4970ef0
Jannis Leidel authored June 11, 2011
19  django/template/base.py
@@ -866,7 +866,7 @@ def filter_function(self, func):
866 866
         self.filters[getattr(func, "_decorated_function", func).__name__] = func
867 867
         return func
868 868
 
869  
-    def simple_tag(self, func=None, takes_context=None):
  869
+    def simple_tag(self, func=None, takes_context=None, name=None):
870 870
         def dec(func):
871 871
             params, xx, xxx, defaults = getargspec(func)
872 872
             if takes_context:
@@ -887,9 +887,10 @@ def render(self, context):
887 887
                         func_args = resolved_vars
888 888
                     return func(*func_args)
889 889
 
890  
-            compile_func = partial(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode)
  890
+            function_name = name or getattr(func, '_decorated_function', func).__name__
  891
+            compile_func = partial(generic_tag_compiler, params, defaults, function_name, SimpleNode)
891 892
             compile_func.__doc__ = func.__doc__
892  
-            self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
  893
+            self.tag(function_name, compile_func)
893 894
             return func
894 895
 
895 896
         if func is None:
@@ -901,7 +902,7 @@ def render(self, context):
901 902
         else:
902 903
             raise TemplateSyntaxError("Invalid arguments provided to simple_tag")
903 904
 
904  
-    def assignment_tag(self, func=None, takes_context=None):
  905
+    def assignment_tag(self, func=None, takes_context=None, name=None):
905 906
         def dec(func):
906 907
             params, xx, xxx, defaults = getargspec(func)
907 908
             if takes_context:
@@ -948,8 +949,9 @@ def compile_func(parser, token):
948 949
                             % (tag_name, params_min, params_max))
949 950
                 return AssignmentNode(params_vars, target_var)
950 951
 
  952
+            function_name = name or getattr(func, '_decorated_function', func).__name__
951 953
             compile_func.__doc__ = func.__doc__
952  
-            self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
  954
+            self.tag(function_name, compile_func)
953 955
             return func
954 956
 
955 957
         if func is None:
@@ -961,7 +963,7 @@ def compile_func(parser, token):
961 963
         else:
962 964
             raise TemplateSyntaxError("Invalid arguments provided to assignment_tag")
963 965
 
964  
-    def inclusion_tag(self, file_name, context_class=Context, takes_context=False):
  966
+    def inclusion_tag(self, file_name, context_class=Context, takes_context=False, name=None):
965 967
         def dec(func):
966 968
             params, xx, xxx, defaults = getargspec(func)
967 969
             if takes_context:
@@ -1003,9 +1005,10 @@ def render(self, context):
1003 1005
                         new_context['csrf_token'] = csrf_token
1004 1006
                     return self.nodelist.render(new_context)
1005 1007
 
1006  
-            compile_func = partial(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode)
  1008
+            function_name = name or getattr(func, '_decorated_function', func).__name__
  1009
+            compile_func = partial(generic_tag_compiler, params, defaults, function_name, InclusionNode)
1007 1010
             compile_func.__doc__ = func.__doc__
1008  
-            self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
  1011
+            self.tag(function_name, compile_func)
1009 1012
             return func
1010 1013
         return dec
1011 1014
 
10  docs/howto/custom-template-tags.txt
@@ -687,6 +687,16 @@ Or, using decorator syntax::
687 687
 For more information on how the ``takes_context`` option works, see the section
688 688
 on :ref:`inclusion tags<howto-custom-template-tags-inclusion-tags>`.
689 689
 
  690
+.. versionadded:: 1.4
  691
+
  692
+If you need to rename your tag, you can provide a custom name for it::
  693
+
  694
+    register.simple_tags(lambda x: x - 1, name='minusone')
  695
+
  696
+    @register.simple_tag(name='minustwo')
  697
+    def some_function(value):
  698
+        return value - 1
  699
+
690 700
 .. _howto-custom-template-tags-assignment-tags:
691 701
 
692 702
 Assignment tags
1  docs/releases/1.4.txt
@@ -175,6 +175,7 @@ Django 1.4 also includes several smaller improvements worth noting:
175 175
   code are slightly emphasized. This change makes it easier to scan a stacktrace
176 176
   for issues in user code.
177 177
 
  178
+* Customizable names for :meth:`~django.template.Library.simple_tag`.
178 179
 
179 180
 .. _backwards-incompatible-changes-1.4:
180 181
 
6  tests/regressiontests/templates/templatetags/custom.py
@@ -114,3 +114,9 @@ def assignment_params_and_context(context, arg):
114 114
     """Expected assignment_params_and_context __doc__"""
115 115
     return "assignment_params_and_context - Expected result (context value: %s): %s" % (context['value'], arg)
116 116
 assignment_params_and_context.anything = "Expected assignment_params_and_context __dict__"
  117
+
  118
+register.simple_tag(lambda x: x - 1, name='minusone')
  119
+
  120
+@register.simple_tag(name='minustwo')
  121
+def minustwo_overridden_name(value):
  122
+    return value - 2
5  tests/regressiontests/templates/tests.py
@@ -1385,6 +1385,11 @@ def get_template_tests(self):
1385 1385
             'templatetag11': ('{% templatetag opencomment %}', {}, '{#'),
1386 1386
             'templatetag12': ('{% templatetag closecomment %}', {}, '#}'),
1387 1387
 
  1388
+            # Simple tags with customized names
  1389
+            'simpletag-renamed01': ('{% load custom %}{% minusone 7 %}', {}, '6'),
  1390
+            'simpletag-renamed02': ('{% load custom %}{% minustwo 7 %}', {}, '5'),
  1391
+            'simpletag-renamed03': ('{% load custom %}{% minustwo_overridden_name 7 %}', {}, template.TemplateSyntaxError),
  1392
+
1388 1393
             ### WIDTHRATIO TAG ########################################################
1389 1394
             'widthratio01': ('{% widthratio a b 0 %}', {'a':50,'b':100}, '0'),
1390 1395
             'widthratio02': ('{% widthratio a b 100 %}', {'a':0,'b':0}, ''),

0 notes on commit d27f909

Please sign in to comment.
Something went wrong with that request. Please try again.