Fixed #12847 -- Added name parameter to simple_tag, assignment_tag and inclusion_tag helpers. Thanks, vladmos.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16373 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2011-06-11 16:05:28 +00:00
parent 4970ef07c2
commit d27f909d2e
5 changed files with 33 additions and 8 deletions

View File

@ -866,7 +866,7 @@ class Library(object):
self.filters[getattr(func, "_decorated_function", func).__name__] = func self.filters[getattr(func, "_decorated_function", func).__name__] = func
return 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): def dec(func):
params, xx, xxx, defaults = getargspec(func) params, xx, xxx, defaults = getargspec(func)
if takes_context: if takes_context:
@ -887,9 +887,10 @@ class Library(object):
func_args = resolved_vars func_args = resolved_vars
return func(*func_args) 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__ compile_func.__doc__ = func.__doc__
self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) self.tag(function_name, compile_func)
return func return func
if func is None: if func is None:
@ -901,7 +902,7 @@ class Library(object):
else: else:
raise TemplateSyntaxError("Invalid arguments provided to simple_tag") 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): def dec(func):
params, xx, xxx, defaults = getargspec(func) params, xx, xxx, defaults = getargspec(func)
if takes_context: if takes_context:
@ -948,8 +949,9 @@ class Library(object):
% (tag_name, params_min, params_max)) % (tag_name, params_min, params_max))
return AssignmentNode(params_vars, target_var) return AssignmentNode(params_vars, target_var)
function_name = name or getattr(func, '_decorated_function', func).__name__
compile_func.__doc__ = func.__doc__ compile_func.__doc__ = func.__doc__
self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) self.tag(function_name, compile_func)
return func return func
if func is None: if func is None:
@ -961,7 +963,7 @@ class Library(object):
else: else:
raise TemplateSyntaxError("Invalid arguments provided to assignment_tag") 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): def dec(func):
params, xx, xxx, defaults = getargspec(func) params, xx, xxx, defaults = getargspec(func)
if takes_context: if takes_context:
@ -1003,9 +1005,10 @@ class Library(object):
new_context['csrf_token'] = csrf_token new_context['csrf_token'] = csrf_token
return self.nodelist.render(new_context) 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__ compile_func.__doc__ = func.__doc__
self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) self.tag(function_name, compile_func)
return func return func
return dec return dec

View File

@ -687,6 +687,16 @@ Or, using decorator syntax::
For more information on how the ``takes_context`` option works, see the section For more information on how the ``takes_context`` option works, see the section
on :ref:`inclusion tags<howto-custom-template-tags-inclusion-tags>`. 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: .. _howto-custom-template-tags-assignment-tags:
Assignment tags Assignment tags

View File

@ -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 code are slightly emphasized. This change makes it easier to scan a stacktrace
for issues in user code. for issues in user code.
* Customizable names for :meth:`~django.template.Library.simple_tag`.
.. _backwards-incompatible-changes-1.4: .. _backwards-incompatible-changes-1.4:

View File

@ -114,3 +114,9 @@ def assignment_params_and_context(context, arg):
"""Expected assignment_params_and_context __doc__""" """Expected assignment_params_and_context __doc__"""
return "assignment_params_and_context - Expected result (context value: %s): %s" % (context['value'], arg) 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__" 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 File

@ -1385,6 +1385,11 @@ class Templates(unittest.TestCase):
'templatetag11': ('{% templatetag opencomment %}', {}, '{#'), 'templatetag11': ('{% templatetag opencomment %}', {}, '{#'),
'templatetag12': ('{% templatetag closecomment %}', {}, '#}'), '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 ######################################################## ### WIDTHRATIO TAG ########################################################
'widthratio01': ('{% widthratio a b 0 %}', {'a':50,'b':100}, '0'), 'widthratio01': ('{% widthratio a b 0 %}', {'a':50,'b':100}, '0'),
'widthratio02': ('{% widthratio a b 100 %}', {'a':0,'b':0}, ''), 'widthratio02': ('{% widthratio a b 100 %}', {'a':0,'b':0}, ''),