简体   繁体   English

Django:如何使用Jinja 2的第三方app模板标签?

[英]Django : How to use 3rd party app templatetags with Jinja 2?

I am trying Jinja2 for my django website. 我正在为我的django网站尝试Jinja2

Now, since Jinja2 is not official django templating engine and its refusing to recognise / load the template tags I was using prior to jinja2 . 现在,由于Jinja2不是官方django templating engine并且拒绝识别/加载我在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. 在这种情况下,似乎不可能使用Jinja2,因为系统必须按照jinja2工作。

[ I am also using coffin as an adapter for Jinja-Django ] [我也使用coffin作为Jinja-Django的适配器]

Any help is appreciated ! 任何帮助表示赞赏!

You can do this with coffin. 你可以用棺材做到这一点。 Coffin supplies a way to register django-style tags to use within jinja2 templates: Coffin提供了一种注册django风格标签的方法,以便在jinja2模板中使用:

from coffin import template
from ThrdPartyDjangoLib import djangoTagIWantToUse
register = template.Library()

register.tag('djangoTagIWantToUse', djangoTagIWantToUse)

According to coffin docs you will have to rewrite any custom django templates tags as custom Jinja2 extensions. 根据棺材文档,您将不得不将任何自定义django模板标记重写为自定义Jinja2扩展。

You could also use jinja2 macros feature to emulate the Django's template tags. 您还可以使用jinja2宏功能来模拟Django的模板标签。 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). 最显着的区别是,对于Jinja2宏,有必要通过模板上下文提供所有上下文数据,而在Django标签中,您可以使用其他方式访问数据(例如从数据库加载或调用其他Python库)。

I've been using Jinja2 templates for a while and never had a need to create a custom template tag. 我一直在使用Jinja2模板,从来没有必要创建自定义模板标签。

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模板,在另一个应用程序中使用jinja2,这不是问题,但是不容易从django模板导入或扩展jinja2模板,反之亦然。

Django's structure does not allow for swapping the template engine since it is a core part of the system. Django的结构不允许交换模板引擎,因为它是系统的核心部分。 Even if you can by using coffin , it is not a supported configuration and no third-party module can be expected to support it. 即使您可以使用coffin ,它也不是受支持的配置,并且不能期望第三方模块支持它。 It would be same as asking third party modules to support sqlalchemy because you found a way to make django work with it. 这与询问第三方模块支持sqlalchemy是一样的,因为你找到了让django使用它的方法。

If you want to use jinja2, use a framework that is designed with a pluggable template engine - or one that comes without a template engine. 如果你想使用jinja2,请使用一个设计有可插拔模板引擎的框架 - 或者没有模板引擎的框架。

The integration page lists out of the box integrations that come with Jinja2. 集成页面列出了Jinja2附带的开箱即用集成。 On that page you can see that Pyramid is supported - and that is because by design pyramid allows for pluggable components. 在该页面上,您可以看到支持Pyramid - 这是因为设计金字塔允许可插入组件。

Flask (made by the same people behind Jinja2 ) has native support for Jinja2. Flask (由Jinja2背后的同一个人制作)对Jinja2有原生支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM