Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#12784 closed (worksforme)

Template tag and filter decorator fix

Reported by: jimhark Owned by: nobody
Component: Documentation Version: 1.1
Severity: Keywords: Template tag filter decorator
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I think the main page for this documentation is:

http://docs.djangoproject.com/en/dev/howto/custom-template-tags/

But other pages may also need to be corrected.

When using register.filter() or register.tag() as a decorator, if you leave off the name argument, you still need the parentheses:

  @register.filter
  @stringfilter
  def lower(value):
      return value.lower()

Must instead be:

  @register.filter()
  @stringfilter
  def lower(value):
      return value.lower()

I figured this out by reading the source.

I notice the Django documentation tends not to be just simple reference, but incorporates lots of examples and best practices. For clarity and performance, I would write this as:

  makefilter = register.filter()
  
  @makefilter
  @stringfilter
  def lower(value):
      return value.lower()

And I'd use a similar approach to register.tag(). I urge you to consider this form when correcting the documentation. However, for the rest of this bug report I ignore this style issue and show the simplest fix.

Here are the rest of the changes:

Change:

  @register.filter
  def myfilter(value):
      return value
  myfilter.is_safe = True

To:

  @register.filter()
  def myfilter(value):
      return value
  myfilter.is_safe = True

Change:

  @register.filter
  def add_xx(value):
      return '%sxx' % value
  add_xx.is_safe = True

To:

  @register.filter()
  def add_xx(value):
      return '%sxx' % value
  add_xx.is_safe = True

Change:

  @register.tag
  def shout(parser, token):

To:

  @register.tag()
  def shout(parser, token):

Thanks,

Jim

Change History (2)

comment:1 by Karen Tracey, 15 years ago

Resolution: worksforme
Status: newclosed

I've not encountered any problem following the current docs, omitting parentheses. Therefore I believe the doc as it currently is is correct: parentheses are not required. What in the source gave you the impression that they are?

in reply to:  1 comment:2 by jimhark, 15 years ago

Replying to kmtracey:

I've not encountered any problem following the current docs, omitting parentheses. Therefore I believe the doc as it currently is is correct: parentheses are not required. What in the source gave you the impression that they are?

Sorry then. Their must be something screwy in my configuration because after adding the parentheses the template system stopped crashing for us. I got the idea after looking at template/_init_.py in the Library.tag function which starts like this:

    def tag(self, name=None, compile_function=None):
        if name == None and compile_function == None:
            # @register.tag()
            return self.tag_function
        elif name != None and compile_function == None:
            if(callable(name)):
                # @register.tag
                return self.tag_function(name)

I read the if and it gave me the idea to add the parens which got my system working. Now reading further I see in the elif clause if "name" is callable it should just work without the parens. I'll be more careful before opening an issue in the future. Also if I can track down exactly what's causing the weird behavior on my system I'll post it here as a cautionary tail for others who might run into the same problem.

Thanks,

Jim

Note: See TracTickets for help on using tickets.
Back to Top