Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#4760 closed (invalid)

BuiltinLibrary class for automatic and easy insertion of custom filters/tags

Reported by: A. J. Ellerton <aj@…> Owned by: nobody
Component: Template system Version: master
Severity: Keywords: Builtin Automatic Library
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no



Re the article here:

(ignoring the digression about email subject lines!)

I put forward the need for a simple way to have filters and tags that I define in my local script automatically available to DJango, without the need for a package import, or a specific 'template' package within my source tree.

I have attached a page of 4 lines (+ documentation) that enables this with the class BuiltinLibrary. Using the class is as follows:

 def filter_rst_h1(value, arg='-'):
    "Write a single underline under the text"
    line=arg * len(value)
    return "%s\n%s" % (value, line)

  from django.template import ``BuiltinLibrary`` # <-- NEW!

  # set up extra library of routines for later use...
  library = BuiltinLibrary()
  library.filter('filter_rst_h1', filter_rst_h1) 

The patch and a full, standalone demonstration program is attached.

Thanks for considering this patch...

Attachments (2)

20070703_BuiltinLibrary.patch (1.0 KB) - added by A. J. Ellerton <aj@…> 12 years ago.
Patch file (3.6 KB) - added by A. J. Ellerton <aj@…> 12 years ago.
Standalone/complete demonstration program

Download all attachments as: .zip

Change History (4)

Changed 12 years ago by A. J. Ellerton <aj@…>

Patch file

Changed 12 years ago by A. J. Ellerton <aj@…>

Attachment: added

Standalone/complete demonstration program

comment:1 Changed 11 years ago by Jacob

Resolution: invalid
Status: newclosed

It's not documented, but you want django.template.add_to_builtins(module) -- it'll add a module containing tags/filters to builtins.

comment:2 Changed 11 years ago by Jacob

I realized I didn't quite give the correct explanation. add_to_builtins takes a string argument, so you'd use something like add_to_buildins('mymodule.templatetags.sometags'). See for a couple of slides on the topic.

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