Django:如何使用Jinja 2的第三方应用模板? [英] Django : How to use 3rd party app templatetags with Jinja 2?

查看:124
本文介绍了Django:如何使用Jinja 2的第三方应用模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的django网站尝试 Jinja2



现在,因为 Jinja2 不是官方的 django模板引擎,它拒绝识别/加载我在 jinja2



现在,即使模板标签创建中需要更改,那么如何在第三方应用程序中反映出来?



在这种情况下,似乎不可能使用Jinja2,因为系统必须按照jinja2工作。



[我也使用 coffin 作为Jinja-Django的适配器]



任何帮助都赞赏!

解决方案

根据 coffin docs ,您将不得不将任何自定义的django模板标签重新编写为自定义的Jinja2扩展名。



您还可以使用jinja2宏功能来模拟Django的模板标签。最显着的区别是,对于Jinja2宏,需要通过模板上下文来提供所有上下文数据,而在Django标签中,您可以使用其他方式访问数据(如从数据库加载或调用其他Python库)。 p>

我一直在使用Jinja2模板一段时间,从来没有需要创建自定义模板标签。



可以在网站上的一个应用程序中使用django模板,在另一个应用程序中使用jinja2,但这不是问题,但是不可能从django模板导入或扩展jinja2模板,反之亦然。


I am trying Jinja2 for my django website.

Now, since Jinja2 is not official django templating engine and its refusing to recognise / load the template tags I was using prior to jinja2.

Now, Even if there has to be a change in the template tags creation, then how is it possible to reflect across the 3rd party apps ?

In that case it seems impossible to use Jinja2 since the system has to work as per jinja2.

[ I am also using coffin as an adapter for Jinja-Django ]

Any help is appreciated !

解决方案

According to coffin docs you will have to rewrite any custom django templates tags as custom Jinja2 extensions.

You could also use jinja2 macros feature to emulate the Django's template tags. The most notable difference is that for Jinja2 macros it will be necessary to provide all the context data via the template context, while in Django tags you can access data using other ways (like loading from the database or calling other Python libraries).

I've been using Jinja2 templates for a while and never had a need to create a custom template tag.

It is possible to use django templates in one app on the site and jinja2 in another app, it is not a problem, but it is not readily possible to import or extend jinja2 templates from django templates and vs versa.

这篇关于Django:如何使用Jinja 2的第三方应用模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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