Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 3rd party app templatetags with Jinja 2?

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 Jjinja2.

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).

like image 822
Yugal Jindle Avatar asked Jun 08 '12 13:06

Yugal Jindle


People also ask

What is the difference between Jinja and Jinja2?

from_string . Jinja 2 provides a Template class that can be used to do the same, but with optional additional configuration. Jinja 1 performed automatic conversion of bytes in a given encoding into unicode objects.

What are Jinja templates used for?

It is a text-based template language and thus can be used to generate any markup as well as source code. The Jinja template engine allows customization of tags, filters, tests, and globals. Also, unlike the Django template engine, Jinja allows the template designer to call functions with arguments on objects.

How do I import Jinja2?

Type "cmd" in the search bar and hit Enter to open the command line. What is this? Type “ pip install jinja2 ” (without quotes) in the command line and hit Enter again. This installs jinja2 for your default Python installation.


2 Answers

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.

like image 104
Evgeny Avatar answered Nov 15 '22 01:11

Evgeny


You can do this with coffin. Coffin supplies a way to register django-style tags to use within jinja2 templates:

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

register.tag('djangoTagIWantToUse', djangoTagIWantToUse)
like image 1
spuriousdata Avatar answered Nov 15 '22 00:11

spuriousdata