Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to use tags from django_vite with Jinja2 #8

Closed
mbacalan opened this issue Sep 14, 2021 · 5 comments
Closed

Unable to use tags from django_vite with Jinja2 #8

mbacalan opened this issue Sep 14, 2021 · 5 comments
Labels
enhancement New feature or request

Comments

@mbacalan
Copy link

mbacalan commented Sep 14, 2021

Hello,

I followed everything in the readme and took a look at the example app too but I can't seem to get it working on our company project. Every tag registered by django_vite, such as load, vite_hmr_client etc. causes an Encountered unknown tag error. I can see the django_vite folder inside venv and it is present in INSTALLED_APPS. The server starts with no errors so it doesn't look like there's an issue with installing the pip package but when I load the page, it flips out.

We are using Jinja template engine via django-jinja package. I'm suspecting that's the cause?

Django Version: 2.2.24
Python Version: 3.9.5

Let me know if you need more information about the project.

Thanks.

@mbacalan mbacalan changed the title Unable to use tags from django_vite Unable to use tags from django_vite with Jinja2 Sep 14, 2021
@MrBin99
Copy link
Owner

MrBin99 commented Sep 14, 2021

Hi,
I'm sorry but django-vite does not support Jinja templating for the moment.
I keep this issue open to maybe add the feature if other people are interested too.
Or feel free to do a PR.

@MrBin99 MrBin99 added the enhancement New feature or request label Sep 14, 2021
@wizpig64
Copy link

Hi @mbacalan,

django-jinja automatically loads django's built-in tags, but it has to be configured to load any third-party tags in your settings, like this:

# settings.py

TEMPLATES = [
    {
        "BACKEND": "django_jinja.backend.Jinja2",
        "OPTIONS": {
            "globals": {
                "vite_hmr_client": "django_vite.templatetags.django_vite.vite_hmr_client",
                "vite_asset": "django_vite.templatetags.django_vite.vite_asset",
            },
            ...

Furthermore, in jinja templates, you call the tags as if they were functions, with {{ }} not {% %}. So if you have a django template like this:

{% load django_vite %}
<html>
<head>
  <title>vite on django</title>
  {% vite_hmr_client %}
</head>
<body>
  <div id="app"></div>
  {% vite_asset 'src/main.js' %}
</body>
</html>

...you would need to convert it to this in a jinja template:

<html>
<head>
  <title>vite on jinja</title>
  {{ vite_hmr_client() }}
</head>
<body>
  <div id="app"></div>
  {{ vite_asset('src/main.js') }}
</body>
</html>

I hope that helps.

@mbacalan
Copy link
Author

@wizpig64 thanks! Works like a charm. Maybe this issue can be closed, unless this is only applicable to django-jinja and using Jinja2 in a different way would still require changes.

@MrBin99
Copy link
Owner

MrBin99 commented Sep 21, 2021

Thanks for your help @wizpig64 !
@mbacalan I let you close this issue, if it's good for you.

@sondrelg
Copy link

sondrelg commented Jun 1, 2022

This was very helpful. I think adding it to the readme would be a good idea 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants