问题
Am going around the houses trying to find a way to implement a simple filter.
I want to create the equivalent of some Smarty "tags" to make porting easier, notably {mail_to} http://www.smarty.net/manual/en/language.function.mailto.php
I seem to be going around in circles between the jinga2 docs http://jinja.pocoo.org/2/documentation/extensions#module-jinja2.ext
and the webhelpers http://pylonshq.com/docs/en/0.9.7/modules/templating/
What I'm expecting to write is something like
{% mail_to user=c.user.email encode='hex' %}
Cant figure out how to piece it all together, ie location of lib and how to load for usage.
tia
回答1:
Write you extension and put it into lib/extensions.py
To load you extension into environment do in config/environment.py:
from MYAPP.lib import extensions
config['pylons.app_globals'].jinja2_env = Environment(loader=ChoiceLoader(
[FileSystemLoader(path) for path in paths['templates']]),
extensions=[extensions.YOU_EXTENSION_MAIL_TO_CLASS]))
# If you extension use some options, you can init it
config['pylons.app_globals'].jinja2_env.mail_to_smtp_host = 'some_host'
After in yours templates simply call {% mail_to arg1, arg2 %}
来源:https://stackoverflow.com/questions/3368597/how-do-i-create-a-jinja2-filter-and-use-it-within-pylons