Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@jezdez jezdez authored
View
19 django/template/base.py
@@ -866,7 +866,7 @@ def filter_function(self, func):
self.filters[getattr(func, "_decorated_function", func).__name__] = func
return func
- def simple_tag(self, func=None, takes_context=None):
+ def simple_tag(self, func=None, takes_context=None, name=None):
def dec(func):
params, xx, xxx, defaults = getargspec(func)
if takes_context:
@@ -887,9 +887,10 @@ def render(self, context):
func_args = resolved_vars
return func(*func_args)
- compile_func = partial(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode)
+ function_name = name or getattr(func, '_decorated_function', func).__name__
+ compile_func = partial(generic_tag_compiler, params, defaults, function_name, SimpleNode)
compile_func.__doc__ = func.__doc__
- self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
+ self.tag(function_name, compile_func)
return func
if func is None:
@@ -901,7 +902,7 @@ def render(self, context):
else:
raise TemplateSyntaxError("Invalid arguments provided to simple_tag")
- def assignment_tag(self, func=None, takes_context=None):
+ def assignment_tag(self, func=None, takes_context=None, name=None):
def dec(func):
params, xx, xxx, defaults = getargspec(func)
if takes_context:
@@ -948,8 +949,9 @@ def compile_func(parser, token):
% (tag_name, params_min, params_max))
return AssignmentNode(params_vars, target_var)
+ function_name = name or getattr(func, '_decorated_function', func).__name__
compile_func.__doc__ = func.__doc__
- self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
+ self.tag(function_name, compile_func)
return func
if func is None:
@@ -961,7 +963,7 @@ def compile_func(parser, token):
else:
raise TemplateSyntaxError("Invalid arguments provided to assignment_tag")
- def inclusion_tag(self, file_name, context_class=Context, takes_context=False):
+ def inclusion_tag(self, file_name, context_class=Context, takes_context=False, name=None):
def dec(func):
params, xx, xxx, defaults = getargspec(func)
if takes_context:
@@ -1003,9 +1005,10 @@ def render(self, context):
new_context['csrf_token'] = csrf_token
return self.nodelist.render(new_context)
- compile_func = partial(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode)
+ function_name = name or getattr(func, '_decorated_function', func).__name__
+ compile_func = partial(generic_tag_compiler, params, defaults, function_name, InclusionNode)
compile_func.__doc__ = func.__doc__
- self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
+ self.tag(function_name, compile_func)
return func
return dec
View
10 docs/howto/custom-template-tags.txt
@@ -687,6 +687,16 @@ Or, using decorator syntax::
For more information on how the ``takes_context`` option works, see the section
on :ref:`inclusion tags<howto-custom-template-tags-inclusion-tags>`.
+.. versionadded:: 1.4
+
+If you need to rename your tag, you can provide a custom name for it::
+
+ register.simple_tags(lambda x: x - 1, name='minusone')
+
+ @register.simple_tag(name='minustwo')
+ def some_function(value):
+ return value - 1
+
.. _howto-custom-template-tags-assignment-tags:
Assignment tags
View
1  docs/releases/1.4.txt
@@ -175,6 +175,7 @@ Django 1.4 also includes several smaller improvements worth noting:
code are slightly emphasized. This change makes it easier to scan a stacktrace
for issues in user code.
+* Customizable names for :meth:`~django.template.Library.simple_tag`.
.. _backwards-incompatible-changes-1.4:
View
6 tests/regressiontests/templates/templatetags/custom.py
@@ -114,3 +114,9 @@ def assignment_params_and_context(context, arg):
"""Expected assignment_params_and_context __doc__"""
return "assignment_params_and_context - Expected result (context value: %s): %s" % (context['value'], arg)
assignment_params_and_context.anything = "Expected assignment_params_and_context __dict__"
+
+register.simple_tag(lambda x: x - 1, name='minusone')
+
+@register.simple_tag(name='minustwo')
+def minustwo_overridden_name(value):
+ return value - 2
View
5 tests/regressiontests/templates/tests.py
@@ -1385,6 +1385,11 @@ def get_template_tests(self):
'templatetag11': ('{% templatetag opencomment %}', {}, '{#'),
'templatetag12': ('{% templatetag closecomment %}', {}, '#}'),
+ # Simple tags with customized names
+ 'simpletag-renamed01': ('{% load custom %}{% minusone 7 %}', {}, '6'),
+ 'simpletag-renamed02': ('{% load custom %}{% minustwo 7 %}', {}, '5'),
+ 'simpletag-renamed03': ('{% load custom %}{% minustwo_overridden_name 7 %}', {}, template.TemplateSyntaxError),
+
### WIDTHRATIO TAG ########################################################
'widthratio01': ('{% widthratio a b 0 %}', {'a':50,'b':100}, '0'),
'widthratio02': ('{% widthratio a b 100 %}', {'a':0,'b':0}, ''),
Please sign in to comment.
Something went wrong with that request. Please try again.