Django

Code

Ticket #1105 (new)

Opened 2 years ago

Last modified 8 months ago

[patch] simple_tag decorator enhancement

Reported by: django@kieranholland.com Assigned to: nobody
Component: Core framework Version:
Keywords: Cc:
Triage Stage: Accepted Has patch: 1
Needs documentation: 1 Needs tests: 1
Patch needs improvement: 1

Description

Attached is a patch for a tag function decorator that I find useful. It's a simple extension of Robert's simple_tag. The tag function is passed the context as the first argument and also any arguments to the tag, resolved in the same way as for the simple_tag decorator. The decorated function can manipulate the context and either return a string, which will be inserted into the template, or None.

For example, this tag puts the project settings into the context:

@register.simple_tag_with_context
def get_settings(context):

    """
    Put the project settings into the context.

    Usage::

        {% get_settings %}
    """

    from django.conf import settings
    context['settings'] = settings

And this one is the equivalent of the existing simple_tag example in NewAdminChanges:

@register.simple_tag_with_context
def monkey_tag(context, verb):
    return "I %s no evil" % verb

Attachments

simple_tag_with_context_r1764.diff (1.2 kB) - added by django@kieranholland.com on 12/21/05 20:39:09.
Patch adds simple_tag_with_context tag function decorator
simple_tag_r1785.diff (3.1 kB) - added by django@kieranholland.com on 12/28/05 00:35:52.
simple_tag enhancement

Change History

12/21/05 20:39:09 changed by django@kieranholland.com

  • attachment simple_tag_with_context_r1764.diff added.

Patch adds simple_tag_with_context tag function decorator

12/27/05 08:40:36 changed by rjwittams

I would prefer this to be similar to the inclusion_tag , ie optionally take an argument of takes_context=1. This is slightly tricky due to how decorators work ( making the no args and args versions work the same). register_tag does this correctly.

Going further, I'd like to split this stuff out a bit more. gen_compile_func should make use of a function attribute, eg arg_info. This would be a list of stuff that could be taken into account when creating the compile function. Decorators should then be provided, eg

@register.simple_tag @takes_context @takes_block def my_tag(context, block, arg1, arg2):

pass

This way the concerns are separated a bit.

12/28/05 00:35:13 changed by django@kieranholland.com

  • summary changed from [patch] simple_tag_with_context decorator to [patch] simple_tag decorator enhancement.

I've reimplemented simple_tag in line with Robert's first suggestion.

Now simple_tag passes in context and/or block arguments (always in that order) if requested. If a block is requested then the tag will automatically consume the template from {% tagname %} until {% endtagname %} passing the resulting nodelist in as the block argument.

The comment tag can now be implemented:

@register.simple_tag(takes_block=True)
def comment(block):
    pass

And the upper tag example from the template documentation becomes:

@register.simple_tag(takes_context=True, takes_block=True)
def upper(context, block):
    return block.render(context).upper()

Robert, I'm not sure if I like the idea of manipulating simple_tag with accessory decorators. As far as I can tell what you are proposing is just a synatactic hack to pass arguments to the simple_tag decorator; the accessory decorators would have no obvious meaning except in the context of simple_tag. I think I prefer the syntax above because there really is only one decorator being applied to the function (but if the original debate about Python decorator syntax is anything to go by then the "right" way will have to be determined by a benevolent dictator;).

12/28/05 00:35:52 changed by django@kieranholland.com

  • attachment simple_tag_r1785.diff added.

simple_tag enhancement

06/12/06 06:09:01 changed by URL

  • type deleted.

09/26/06 22:14:08 changed by mtredinnick

  • owner changed from adrian to mtredinnick.
  • type set to defect.

02/05/07 13:56:41 changed by SmileyChris

  • needs_docs set to 1.
  • needs_tests set to 1.
  • stage changed from Unreviewed to Accepted.

Seems like a useful enhancement.

09/14/07 11:37:20 changed by racter

  • needs_better_patch set to 1.

patch doesn't apply anymore


Add/Change #1105 ([patch] simple_tag decorator enhancement)




Change Properties
Action