Ticket #12847: renamed_simple_tags.2.patch

File renamed_simple_tags.2.patch, 4.2 KB (added by vladmos, 4 years ago)
  • docs/howto/custom-template-tags.txt

     
    687687For more information on how the ``takes_context`` option works, see the section
    688688on :ref:`inclusion tags<howto-custom-template-tags-inclusion-tags>`.
    689689
     690.. versionadded:: 1.4
     691
     692If 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
    690700.. _howto-custom-template-tags-assignment-tags:
    691701
    692702Assignment tags
  • docs/releases/1.4.txt

     
    166166  code are slightly emphasized. This change makes it easier to scan a stacktrace
    167167  for issues in user code.
    168168
     169* Customizable names for :meth:`~django.template.Library.simple_tag`.
    169170
    170171.. _backwards-incompatible-changes-1.4:
    171172
  • django/template/base.py

     
    866866        self.filters[getattr(func, "_decorated_function", func).__name__] = func
    867867        return func
    868868
    869     def simple_tag(self, func=None, takes_context=None):
     869    def simple_tag(self, func=None, takes_context=None, name=None):
    870870        def dec(func):
    871             params, xx, xxx, defaults = getargspec(func)
     871            params, varargs, kwargs, defaults = getargspec(func)
    872872            if takes_context:
    873873                if params[0] == 'context':
    874874                    params = params[1:]
     
    887887                        func_args = resolved_vars
    888888                    return func(*func_args)
    889889
    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)
    891892            compile_func.__doc__ = func.__doc__
    892             self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
     893            self.tag(function_name, compile_func)
    893894            return func
    894895
    895896        if func is None:
  • tests/regressiontests/templates/tests.py

     
    13851385            'templatetag11': ('{% templatetag opencomment %}', {}, '{#'),
    13861386            'templatetag12': ('{% templatetag closecomment %}', {}, '#}'),
    13871387
     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
    13881393            ### WIDTHRATIO TAG ########################################################
    13891394            'widthratio01': ('{% widthratio a b 0 %}', {'a':50,'b':100}, '0'),
    13901395            'widthratio02': ('{% widthratio a b 100 %}', {'a':0,'b':0}, ''),
  • tests/regressiontests/templates/templatetags/custom.py

     
    114114    """Expected assignment_params_and_context __doc__"""
    115115    return "assignment_params_and_context - Expected result (context value: %s): %s" % (context['value'], arg)
    116116assignment_params_and_context.anything = "Expected assignment_params_and_context __dict__"
     117
     118register.simple_tag(lambda x: x - 1, name='minusone')
     119
     120@register.simple_tag(name='minustwo')
     121def minustwo_overridden_name(value):
     122    return value - 2
Back to Top