How do I create a jinja2 filter and use it within pylons?

时间秒杀一切 提交于 2019-12-24 11:11:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!