On Tue, 2007-06-12 at 05:44 -0400, Andrew R wrote:
> James Bennett wrote:
> > So long as your code is in a location where your code can be imported
> > by Python, there is another way; there are a couple of undocumented
> > functions in the template system which handle the loading of tag
> > libraries. 
> > [snip]
> > So something like
> > 
> >     from django.template import add_to_builtins
> >     add_to_builtins('path.to.your.library')
> 
> Good tip - thanks!
> 
> I have to admit, though, that I think adding a custom filter should be as 
> simple
> as this code alone:
> 
> > from django.template import Library
> >
> > register = Library()
> >
> > @register.filter
> > def javadoc_filter(value, arg):
> >    return "whatever"
> 
> I think part of my confusion was that my subconscious thinking was "my filter
> code has been imported (I can see the .pyc) so I know python has seen it, and
> therefore the filter is registered with the library, so I should be able to 
> use
> it.".
> 
> This thinking does not reflect the current reality, and thats ok because I 
> have
> a workaround. But I think there is merit in a plain approach like that.
> 
> After all - Django is into the D.R.Y. principle, so having to use
> register.filter() *as well* as have to have the module in a certain directory,
> seems a bit repetitious.

If your custom filter is in the templatetags directory of an installed
Django app, what you have done is all you have to do. Then you use the
"load" template tag to load the filter file and you can use it happily
(and that is why it has to be in the templatetags directory). The
reasons you were having trouble was because you said you didn't want to
do this for some reason. The trick James pointed you to is a way to add
the filter to the global namespace of available filters so that you
don't have to load it.

If you come up with a patch to make filter registration remember the
path to the file so that the "load" template tag can find it later, we'd
certainly consider it.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to