Skip to content

Commit

Permalink
Fixed #12847 -- Added name parameter to simple_tag, assignment_tag an…
Browse files Browse the repository at this point in the history
…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
jezdez committed Jun 11, 2011
1 parent 4970ef0 commit d27f909
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
19 changes: 11 additions & 8 deletions django/template/base.py
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions docs/howto/custom-template-tags.txt
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/releases/1.4.txt
Expand Up @@ -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:

Expand Down
6 changes: 6 additions & 0 deletions tests/regressiontests/templates/templatetags/custom.py
Expand Up @@ -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
5 changes: 5 additions & 0 deletions tests/regressiontests/templates/tests.py
Expand Up @@ -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}, ''),
Expand Down

0 comments on commit d27f909

Please sign in to comment.