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

Documentation on how Jinja's env can be re-used for JinjaX in a Django setup #51

Merged
merged 3 commits into from
Apr 22, 2024

Conversation

AhnafCodes
Copy link
Contributor

The Key to using JInjaX with Django is changing jinja2.env.py and adding JinjaX as environment extension and reusing the jinja env

env.add_extension(JinjaX)
catalog = Catalog(jinja_env=env)

Adding how env for Jinja can be reused for JinjaX with Django
formatting
env.add_extension(JinjaX)
catalog = Catalog(jinja_env=env)
catalog.add_folder("components")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a nicer way that automatically uses the components subdirectory for each jinja2 path that Django uses when it passes the loader:

import jinjax
from jinja2.loaders import FileSystemLoader


def environment(loader: FileSystemLoader, **options):
    env = Environment(loader=loader, **options)

    ...

    env.add_extension(jinjax.JinjaX)
    catalog = jinjax.Catalog(jinja_env=env)
    for dir in loader.searchpath:
        catalog.add_folder(os.path.join(dir, "components"))

    return env

Copy link
Contributor Author

@AhnafCodes AhnafCodes Apr 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, it would be good have a separate "components" folder for shared components and also have "components" subfolder at django-app level. so, it should be:

import jinjax
from jinja2.loaders import FileSystemLoader


def environment(loader: FileSystemLoader, **options):
    env = Environment(loader=loader, **options)

    ...

    env.add_extension(jinjax.JinjaX)
    catalog = jinjax.Catalog(jinja_env=env)

    catalog.add_folder("components")
    for dir in loader.searchpath:
        catalog.add_folder(os.path.join(dir, "components"))

    return env

Copy link

@SmileyChris SmileyChris Apr 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure the root catalog.add_folder("components") is really necessary since loader.searchpath will contain all DIRS (and each app's jinja2 folder if APP_DIRS: True).

To clarify the behaviour here, the app component paths end up as <app>/jinja2/components and the DIRS component paths will append components to each of them (usually <projectroot>/jinja2/components or <projectroot>/templates/components). Personally I like that nesting...

it would be good have a separate "components" folder for shared components and also have "components" subfolder at each django-app level.

Changes made based on feedback from  @SmileyChris  
jpsca#51 (comment)
@jpsca jpsca force-pushed the main branch 2 times, most recently from d61dc7f to 7ec71c5 Compare April 19, 2024 23:45
@jpsca jpsca merged commit 7c8d4ee into jpsca:main Apr 22, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants