Re: Adding custom filter to stand-alone template

2011-05-05 Thread Up2L8


On May 4, 7:10 pm, Ethan Jucovy  wrote:
> On Wed, May 4, 2011 at 7:19 PM, Ethan Jucovy  wrote:
> > This also feels a little hacky, but IMHO cleaner than messing with
> >> builtins.
>
> Looking at django/template/__init__.py -- it does seem like INSTALLED_APPS
> and django.template.builtins are the only ways to do this.

Thanks for the validation Ethan.  I know this isn't the primary
development style as far as django goes, so I'm not too surprised this
use case is a bit rough around the edges.
Maybe I'll file an enhancement and work on a patch.

Eric

-- 
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.



Re: Adding custom filter to stand-alone template

2011-05-04 Thread Ethan Jucovy
On Wed, May 4, 2011 at 7:19 PM, Ethan Jucovy  wrote:

> This also feels a little hacky, but IMHO cleaner than messing with
>> builtins.
>
>
Looking at django/template/__init__.py -- it does seem like INSTALLED_APPS
and django.template.builtins are the only ways to do this.

-Ethan

-- 
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.



Re: Adding custom filter to stand-alone template

2011-05-04 Thread Ethan Jucovy
On Wed, May 4, 2011 at 6:41 PM, Up2L8  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.



Adding custom filter to stand-alone template

2011-05-04 Thread Up2L8
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), 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.

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.