Opened 17 years ago

Closed 17 years ago

Last modified 17 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: dev
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

Description

All,

Re the article here:

[http://groups.google.com/group/django-users/browse_frm/thread/2f254591d5d84340/5ad372019ee0c3c6?lnk=gst&q=custom+filters&rnum=1#5ad372019ee0c3c6
]
(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...
A.J.E.

Attachments (2)

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

Download all attachments as: .zip

Change History (4)

by A. J. Ellerton <aj@…>, 17 years ago

Patch file

by A. J. Ellerton <aj@…>, 17 years ago

Attachment: demo_django_filter.py added

Standalone/complete demonstration program

comment:1 by Jacob, 17 years ago

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 by Jacob, 17 years ago

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 http://toys.jacobian.org/presentations/2007/oscon/tutorial/#s101 for a couple of slides on the topic.

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