On Wed, May 4, 2011 at 6:41 PM, Up2L8 <miller...@gmail.com> wrote: > I'm trying to use the Template library outside of a normal Django > webapp and am having difficulty figuring out the right way to register > a custom filter. > The current implementation works, but I don't like accessing the guts > of the template module (see below),
Not sure what you mean by "accessing the guts of the template module". Aside from the builtin hack (see below), everything you're doing uses the documented django.template APIs. or calling my custom Library a > "builtin". Whats the right way to do this? If the answer is "use a different > templating system" then I'm fine with that too... the right tool for > the right job and all that. > The way I do this is by faking a django app, with a ./templatetags/ directory containing my template tags, and configuring INSTALLED_APPS in the settings: {{{ ## main-file.py from django.conf import settings settings.configure(INSTALLED_APPS=("myapp",)) ## myapp/templatetags/my_filters.py from django import template register = template.Library() @register.filter def rev(stuff): return stuff[::-1] }}} .. I can then {% load my_filters %} in templates, just as I would if it were a full django environment. This also feels a little hacky, but IMHO cleaner than messing with builtins. -Ethan Eric > --- > #!/bin/env python2.7 > import django > import django.template > > django.conf.settings.configure() > > register = django.template.Library() > @register.filter > def rev(stuff): > return stuff[::-1] > > django.template.builtins.append(register) #Eww.. there has to be a > better way! > > tmpl = django.template.Template('{{ "abdc"|rev }}') > print tmpl.render(django.template.Context({})) > > -- > 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 > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- 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 django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.