Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
adding filters to jinja : templatetools
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  17 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Remi Jolin  
View profile  
 More options Sep 29 2012, 6:10 am
From: Remi Jolin <remi.jo...@sysgroup.fr>
Date: Sat, 29 Sep 2012 12:09:48 +0200
Local: Sat, Sep 29 2012 6:09 am
Subject: adding filters to jinja : templatetools
Hello,

I want to add a custom filter to jinja2 and I found that there is a way
to do that through adding a .lib.templatetools.jinja_filters.py module,
so I tryed it...
Every function defined in this module become a filter. But not only
functions :-(, everything imported in this module is included in the
filter's dict !

I'm not sure this was the expected behavior.

Could not it be a .lib.templatetools.py that would contain functions and
a dict named jinja_filters that would contain the filters like in the
config file ? jinja_filters = dict(filter_name=filter_fct, ...) ?

The only change in tg.configuration.app_config.py
change :
         # Try to load custom filters module under
app_package.lib.templatetools
         try:
             filter_package = self.package.__name__ + ".lib.templatetools"
             autoload_lib = __import__(filter_package, {}, {},
['jinja_filters'])
             autoload_filters = autoload_lib.jinja_filters.__dict__
         except (ImportError, AttributeError):
             autoload_filters = {}

by :
         # Try to load custom filters module under
app_package.lib.templatetools
         try:
             filter_package = self.package.__name__ + ".lib.templatetools"
             autoload_lib = __import__(filter_package, {}, {},
['jinja_filters'])
             autoload_filters =
autoload_lib.jinja_filters                                            #
suppress the .__dict__
         except (ImportError, AttributeError):
             autoload_filters = {}

or

         # Try to load custom filters module under
app_package.lib.templatetools
         try:
             filter_package = self.package.__name__ + ".lib.templatetools"
             autoload_lib = __import__(filter_package, {}, {},
['jinja_filters'])
             try:
                 autoload_filters = autoload_lib.jinja_filters.__dict__
             except AttributeError:
                 autoload_filters = autoload_lib.jinja_filters
         except (ImportError, AttributeError):
             autoload_filters = {}

to support both...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alessandro Molina  
View profile  
 More options Sep 29 2012, 9:14 am
From: Alessandro Molina <alessandro.mol...@gmail.com>
Date: Sat, 29 Sep 2012 15:14:13 +0200
Local: Sat, Sep 29 2012 9:14 am
Subject: Re: [TurboGears] adding filters to jinja : templatetools
I'm not actually sure, but shouldn't defining __all__ inside your
jinja_filter.py solve the issue?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Remi Jolin  
View profile  
 More options Sep 29 2012, 9:53 am
From: Remi Jolin <remi.jo...@sysgroup.fr>
Date: Sat, 29 Sep 2012 15:53:11 +0200
Local: Sat, Sep 29 2012 9:53 am
Subject: Re: [TurboGears] adding filters to jinja : templatetools
No, I tried this...  It does nothing but adding one more filter
'__all__' to the filters' list. The __dict__ of an imported module
containes all the definitions, imported modules, etc.

Le 29/09/2012 15:14, Alessandro Molina a crit :


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alessandro Molina  
View profile  
 More options Sep 29 2012, 11:13 am
From: Alessandro Molina <alessandro.mol...@gmail.com>
Date: Sat, 29 Sep 2012 17:13:03 +0200
Local: Sat, Sep 29 2012 11:13 am
Subject: Re: [TurboGears] adding filters to jinja : templatetools
Ah, right, I saw right now that it imports the module itself not using
from form.

Probably the most pythonic way to solve the issue is change the jinja
filters setup to care about __all__, in the mean time you can simply
write your jinja filters inside something like jinja_filters_impl.py
and then just do from jinja_filter_impl import whatyouwant into
.lib.templatetools.jinja_filters


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carlos Daniel Ruvalcaba Valenzuela  
View profile  
 More options Sep 29 2012, 12:31 pm
From: Carlos Daniel Ruvalcaba Valenzuela <clsdan...@gmail.com>
Date: Sat, 29 Sep 2012 09:31:53 -0700
Local: Sat, Sep 29 2012 12:31 pm
Subject: Re: [TurboGears] adding filters to jinja : templatetools
Seems that using __import__ function does not setup imported namespace
as does the sintax "for module import *", thus the dict of the module
includes any other imports.

A quick solution for now could be trying to get the __all__ directly
from the filters module (this works if you defined it) with a fallback
to the __dict__ dictionary checking to get only callables. I have this
working and could push to my fork if there is no better suggestion and
then send a pull request.

Regards,
Carlos Daniel Ruvalcaba Valenzuela


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Remi Jolin  
View profile  
 More options Sep 29 2012, 12:36 pm
From: Remi Jolin <remi.jo...@sysgroup.fr>
Date: Sat, 29 Sep 2012 18:36:32 +0200
Local: Sat, Sep 29 2012 12:36 pm
Subject: Re: [TurboGears] adding filters to jinja : templatetools
Le 29/09/2012 17:13, Alessandro Molina a crit :
> Ah, right, I saw right now that it imports the module itself not using
> from form.

> Probably the most pythonic way to solve the issue is change the jinja
> filters setup to care about __all__, in the mean time you can simply
> write your jinja filters inside something like jinja_filters_impl.py
> and then just do from jinja_filter_impl import whatyouwant into
> .lib.templatetools.jinja_filters

All other jinja filters configuration is done by defining a dict :

base_config.jinja_filters  =  {'my_filter':  my_filter,  'other_filter_function':  other_filter_function}

so having to define a jinja_filters dict in templatetools.py (or
templatetools.__init__.py) does not seem to be a bad idea to me.

Much simpler than the current solution (so I would say more pythonic ;-) )
Is the templatetools directory supposed to contain something else than
jinja_filters.py (AND __init__.py or it doesn't work !) ?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Remi Jolin  
View profile  
 More options Sep 29 2012, 12:45 pm
From: Remi Jolin <remi.jo...@sysgroup.fr>
Date: Sat, 29 Sep 2012 18:45:22 +0200
Local: Sat, Sep 29 2012 12:45 pm
Subject: Re: [TurboGears] adding filters to jinja : templatetools
Le 29/09/2012 18:31, Carlos Daniel Ruvalcaba Valenzuela a crit :
> Seems that using __import__ function does not setup imported namespace
> as does the sintax "for module import *", thus the dict of the module
> includes any other imports.

> A quick solution for now could be trying to get the __all__ directly
> from the filters module (this works if you defined it) with a fallback
> to the __dict__ dictionary checking to get only callables. I have this
> working and could push to my fork if there is no better suggestion and
> then send a pull request.

I don't think it is enough to check callables in __dict__. You may have
imported some functions or other callable items in jinja_filters.py.
And you also have to have an (empty) __init__.py in templatetools to get
everything working.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carlos Daniel Ruvalcaba Valenzuela  
View profile  
 More options Sep 29 2012, 12:46 pm
From: Carlos Daniel Ruvalcaba Valenzuela <clsdan...@gmail.com>
Date: Sat, 29 Sep 2012 09:46:16 -0700
Local: Sat, Sep 29 2012 12:46 pm
Subject: Re: [TurboGears] adding filters to jinja : templatetools

> All other jinja filters configuration is done by defining a dict :

> base_config.jinja_filters  =  {'my_filter':  my_filter,
> 'other_filter_function':  other_filter_function}

> so having to define a jinja_filters dict in templatetools.py (or
> templatetools.__init__.py) does not seem to be a bad idea to me.

> Much simpler than the current solution (so I would say more pythonic ;-) )
> Is the templatetools directory supposed to contain something else than
> jinja_filters.py (AND __init__.py or it doesn't work !) ?

If you define a templatetools inside lib with an __init__.py, when
choosing jinja templates the code will check if there is a
jinja_filters.py module, if there is it will try to import it,
however, the problem here seems to be that this code pulls also
uneeded stuff, image you import the sys module, that will get pulled
into the filter namespace too.

The pythonic solution for this (on modules in general), is to define
the special __all__ dictionary, which tells the importer which
elements of the module should import only, but this only works if you
are using the following import form:

from module import *

We cannot do this on the setup code, we have to rely on tools such as
__module__ which does not do that work for us, but we can try to check
for __all__ and do it manually (which is my proposal.

Regards,
Carlos Daniel Ruvalcaba Valenzuela


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carlos Daniel Ruvalcaba Valenzuela  
View profile  
 More options Sep 29 2012, 12:51 pm
From: Carlos Daniel Ruvalcaba Valenzuela <clsdan...@gmail.com>
Date: Sat, 29 Sep 2012 09:51:14 -0700
Local: Sat, Sep 29 2012 12:51 pm
Subject: Re: [TurboGears] adding filters to jinja : templatetools

> I don't think it is enough to check callables in __dict__. You may have
> imported some functions or other callable items in jinja_filters.py.
> And you also have to have an (empty) __init__.py in templatetools to get
> everything working.

You are right, checking for callables is not enough, but after all a
filter is just a function, even if you can decorate them with
@contextfilter and whatnot, thus the most sensible option (in my
opinion) is to have everybody use __all__. Some times you don't import
anything or have global functions (like when creating simple string
filters), thus maybe using __all__ would not be necessary, the
namespace will not be polluted in this specific case.

You need to have the __init__.py as templatetools acts as a module and
that is the pythonic way, same as controllers is a module (has an
__init__.py) even tough we only use the submodules (such as root.py).

Regards,
Carlos Daniel Ruvalcaba Valenzuela


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Remi Jolin  
View profile  
 More options Sep 29 2012, 12:57 pm
From: Remi Jolin <remi.jo...@sysgroup.fr>
Date: Sat, 29 Sep 2012 18:57:04 +0200
Local: Sat, Sep 29 2012 12:57 pm
Subject: Re: [TurboGears] adding filters to jinja : templatetools
Le 29/09/2012 18:46, Carlos Daniel Ruvalcaba Valenzuela a crit :
>> All other jinja filters configuration is done by defining a dict :

>> base_config.jinja_filters  =  {'my_filter':  my_filter,
>> 'other_filter_function':  other_filter_function}

>> so having to define a jinja_filters dict in templatetools.py (or
>> templatetools.__init__.py) does not seem to be a bad idea to me.

>> Much simpler than the current solution (so I would say more pythonic ;-) )
>> Is the templatetools directory supposed to contain something else than
>> jinja_filters.py (AND __init__.py or it doesn't work !) ?
> If you define a templatetools inside lib with an __init__.py, when
> choosing jinja templates the code will check if there is a
> jinja_filters.py module, if there is it will try to import it,

And if you forget the __init__.py file, the code won't import the
existing jinja_filters.py... (without any notice)
> however, the problem here seems to be that this code pulls also
> uneeded stuff, image you import the sys module, that will get pulled
> into the filter namespace too.

Yes, that's what I discovered.
> The pythonic solution for this (on modules in general), is to define
> the special __all__ dictionary, which tells the importer which
> elements of the module should import only, but this only works if you
> are using the following import form:

> from module import *

> We cannot do this on the setup code, we have to rely on tools such as
> __module__ which does not do that work for us, but we can try to check
> for __all__ and do it manually (which is my proposal.

or import a lib.templatetools.py file containg a jinja_filters dict made
on the same model as the

base_config.jinja_filters . This is my proposal :-)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carlos Daniel Ruvalcaba Valenzuela  
View profile  
 More options Sep 29 2012, 1:09 pm
From: Carlos Daniel Ruvalcaba Valenzuela <clsdan...@gmail.com>
Date: Sat, 29 Sep 2012 10:09:33 -0700
Local: Sat, Sep 29 2012 1:09 pm
Subject: Re: [TurboGears] adding filters to jinja : templatetools

> And if you forget the __init__.py file, the code won't import the existing
> jinja_filters.py... (without any notice)

You're right, that is missing on the docs, I will try to get on this too.

> or import a lib.templatetools.py file containg a jinja_filters dict made on
> the same model as the

> base_config.jinja_filters . This is my proposal :-)

When autoloading was first implemented there was a discussion on how
to do it, if I recall correctly, the reason to use
templatetools/jinja_filters.py is to not pollute the lib directory and
to have separate files for each template type so that genshi will not
import jinja stuff which does not support, thus in theory you can have
templatetools/genshi_filters.py and templatetools/jinja_filters.py  at
the same time, this however, is not supported right now as Genshi,
Mako and Jinja have very different ways of handling this stuff
(special filter functions vs plain functions).

Perhaps Alessandro or Michael can comment better on this.

Regards,
Carlos Daniel Ruvalcaba Valenzuela


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Remi Jolin  
View profile  
 More options Sep 29 2012, 2:58 pm
From: Remi Jolin <remi.jo...@sysgroup.fr>
Date: Sat, 29 Sep 2012 20:58:21 +0200
Local: Sat, Sep 29 2012 2:58 pm
Subject: Re: [TurboGears] adding filters to jinja : templatetools
Le 29/09/2012 19:09, Carlos Daniel Ruvalcaba Valenzuela a crit :
> When autoloading was first implemented there was a discussion on how
> to do it, if I recall correctly, the reason to use
> templatetools/jinja_filters.py is to not pollute the lib directory and
> to have separate files for each template type so that genshi will not
> import jinja stuff which does not support, thus in theory you can have
> templatetools/genshi_filters.py and templatetools/jinja_filters.py  at
> the same time, this however, is not supported right now as Genshi,
> Mako and Jinja have very different ways of handling this stuff
> (special filter functions vs plain functions).

ok, I understand.

This could work for both solutions : templatetools/jinja_filters.py with
mandatory __all__ containing the filters' list and templatetools.py
contianing a jinja_filters dict...

         # Try to load custom filters module under
app_package.lib.templatetools
         try:
             filter_package = self.package.__name__ + ".lib.templatetools"
             autoload_lib = __import__(filter_package, {}, {},
['jinja_filters'])
             try:
                 filters_list = autoload_lib.jinja_filters.__all__
                 autoload_filters = dict([(k,
autoload_lib.jinja_filters.__dict__[k]) for k in filters_list])
             except AttributeError:
                 autoload_filters = autoload_lib.jinja_filters
         except (ImportError, AttributeError):
             autoload_filters = {}


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Pedersen  
View profile  
 More options Oct 1 2012, 11:37 pm
From: Michael Pedersen <m.peder...@icelus.org>
Date: Mon, 1 Oct 2012 23:37:21 -0400
Local: Mon, Oct 1 2012 11:37 pm
Subject: Re: [TurboGears] adding filters to jinja : templatetools
On Sat, Sep 29, 2012 at 1:09 PM, Carlos Daniel Ruvalcaba Valenzuela

<clsdan...@gmail.com> wrote:
> Perhaps Alessandro or Michael can comment better on this.

I know that I've got nothing new to add. You seem to have covered the
bases very well. If you'll prepare a pull request, I'll help it get
reviewed and merged (as will Alessandro, I'm sure).

--
Michael J. Pedersen
My Online Resume: http://www.icelus.org/ -- Google+ http://plus.ly/pedersen
Google Talk: m.peder...@icelus.org -- Twitter: pedersentg


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carlos Daniel Ruvalcaba Valenzuela  
View profile  
 More options Sep 29 2012, 3:58 pm
From: Carlos Daniel Ruvalcaba Valenzuela <clsdan...@gmail.com>
Date: Sat, 29 Sep 2012 12:58:48 -0700
Local: Sat, Sep 29 2012 3:58 pm
Subject: Re: [TurboGears] adding filters to jinja : templatetools
I pushed to my development branch something very similar, but would
like some input from core developers before getting to the pull
request.

https://github.com/clsdaniel/tg2/commit/7cc62f9130fc9651e7258490874bb...

I'm not sure about having a dictionary inside the __init__.py called
jinja_filters, if you have the file and define the dict on __init__.py
you will not be able to import jinja_filters.py the way we are doing
it right now (via __import__), but we can add a special case to manage
that. Keeping it simple seems to be the way to go in my opinion.

Perhaps the jinja quickstart template could include
templatetools/__init__.py and jinja_filter.py files with some comments
to guide the developer.

Regards,
Carlos Daniel Ruvalcaba Valenzuela


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "adding filters to jinja : templatetools - more changes to app_config.py" by Remi Jolin
Remi Jolin  
View profile  
 More options Oct 2 2012, 3:10 pm
From: Remi Jolin <remi.jo...@sysgroup.fr>
Date: Tue, 02 Oct 2012 21:10:00 +0200
Subject: Re: [TurboGears] adding filters to jinja : templatetools - more changes to app_config.py
Carlos,

While you are making changes to app_config.py could you add a new
configuration option to be able define autoescape at the main level :

         if not 'jinja_filters' in self:
             self.jinja_filters = {}

         if not 'jinja_autoescape' in self:
             self.jinja_autoescape = False

         loader = ChoiceLoader(
             [TemplateLoader(path) for path in self.paths['templates']])

         config['pylons.app_globals'].jinja2_env = Environment(
                  autoescape=self.jinja_autoescape, loader=loader,
                  auto_reload=self.auto_reload_templates,
extensions=self.jinja_extensions)

so we can define
base_config.jinja_extensions = ['jinja2.ext.with_', 'jinja2.ext.autoescape']
base_config.jinja_autoescape = True
  in app_cfg.py

or perhaps there is a better option to achieve this.

Le 29/09/2012 21:58, Carlos Daniel Ruvalcaba Valenzuela a crit :


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alessandro Molina  
View profile  
 More options Oct 3 2012, 5:21 am
From: Alessandro Molina <alessandro.mol...@gmail.com>
Date: Wed, 3 Oct 2012 11:21:25 +0200
Local: Wed, Oct 3 2012 5:21 am
Subject: Re: [TurboGears] adding filters to jinja : templatetools - more changes to app_config.py
Currently Jinja autoescape has been set to True for upcoming releases
by default to match the behavior all the other template engines have.
I agree that an option would be great, but the default behavior should
be to enable autoscaping to be coherent with the current behavior for
the other template engines.

It probably also make sense to make this options unique for all the
template engines (much like the templates reload is) as it is probably
rare that the user is going to work with escaping enable on some
template engines and disable on others. Then we can provide a template
engine specific option to override it.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Remi Jolin  
View profile  
 More options Oct 3 2012, 5:36 am
From: Remi Jolin <remi.jo...@sysgroup.fr>
Date: Wed, 03 Oct 2012 11:36:15 +0200
Local: Wed, Oct 3 2012 5:36 am
Subject: Re: [TurboGears] adding filters to jinja : templatetools - more changes to app_config.py
Le 03/10/2012 11:21, Alessandro Molina a crit :
> Currently Jinja autoescape has been set to True for upcoming releases
> by default to match the behavior all the other template engines have.
> I agree that an option would be great, but the default behavior should
> be to enable autoscaping to be coherent with the current behavior for
> the other template engines.

Yes of course (that's why I started looking at this issue :-) ). I
initialized it to False as it was the default in the "official" TG2.2
and caming from TG1/genshi I was used to having autoescape enabled.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »