如何创建jinja2过滤器并在挂架中使用? [英] How do I create a jinja2 filter and use it within pylons?

查看:42
本文介绍了如何创建jinja2过滤器并在挂架中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正四处逛逛,试图找到一种实现简单过滤器的方法.

Am going around the houses trying to find a way to implement a simple filter.

我想创建一些Smarty标记"的等效项以简化移植,尤其是 {mail_to} http://www.smarty.net/manual/en /language.function.mailto.php

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

我似乎在jinga2文档之间转圈了 http://jinja.pocoo.org/2/documentation/extensions# module-jinja2.ext

I seem to be going around in circles between the jinga2 docs http://jinja.pocoo.org/2/documentation/extensions#module-jinja2.ext

和网络助手 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' %}

无法弄清楚如何将它们拼凑在一起,即lib的位置以及如何加载以供使用.

Cant figure out how to piece it all together, ie location of lib and how to load for usage.

tia

推荐答案

编写扩展名并将其放入lib/extensions.py

Write you extension and put it into lib/extensions.py

要将您的扩展程序加载到环境中,请在config/environment.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'

在您的模板中之后,只需调用{%mail_to arg1,arg2%}

After in yours templates simply call {% mail_to arg1, arg2 %}

这篇关于如何创建jinja2过滤器并在挂架中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆