Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Question re how JinjaX interacts with Jinja2 pre-compiled templates (ModuleLoader) #73

Closed
anentropic opened this issue Jun 26, 2024 · 2 comments

Comments

@anentropic
Copy link

anentropic commented Jun 26, 2024

I have a Django project where we are pre-compiling the Jinja templates to minimise cold-start delays when running in AWS Lambda

i.e. we call env.compile_templates as a build step and then use Jinja ModuleLoader as the template loader pointing to the pre-compiled modules

my assumption is that this is the same as Jinja2 'caching' but is effectively pre-warming the cache

I want to introduce JinjaX to the project but I am a bit uncertain how it interacts with our existing Jinja machinery

This is quite similar to the comment here #30 (comment)

I can see that JinjaX caching involves serializing the component def to a dict of props f3c9361#diff-c89d0d43fde46ff29ff599a61650dc9c78e6d1772271bbad01c4295e1a5b5c4aR265

My best guess would be that this is orthogonal to Jinja2 pre-compilation, and probably unnecessary for my case if we are pre-compiling...?

i.e. if JinjaX is just a Jinja extension and our templates include JinjaX components, then when we pre-compile the templates hopefully all the JinjaX stuff is resolved into raw Jinja ahead of time ...could you say if that seems correct?

@anentropic
Copy link
Author

Hmm... looks like JinjaX keeps its own internal Jinja env though, so maybe to the outer template the JinjaX component is a black box, which is going to load and render its own templates at render time

@jpsca
Copy link
Owner

jpsca commented Jun 27, 2024

JinjaX currently works as a pre-processor, replacing all:

<Component attr="value">content</Component>

with function calls like:

{% call catalog.irender("Component", attr="value") %}content{% endcall %}

These calls are evaluated at render time. Each call loads the source of the component file, parses it to extract the names of CSS/JS files, required and/or optional attributes, pre-processes the template (replacing components with function calls, as before), and finally renders the new template.

The rendering still uses the Jinja cache, but the loading and parsing did not, as it was repeated multiple times if a component was used multiple times (like a <Button>). Hence, I added a custom cache in #30. The Jinja cache is still used when rendering the template.

The custom cache depends on the modified time of the component files. If you know the component file is not going to change while the application is running, such as in a production environment, you can create the catalog with auto_reload=False to deactivate the check.

I don't think the situation is bad for production... but it could be better by having a catalog.compile_templates() function that could insert the code of the components recursively, instead of just a call, so we could rely only in the Jinja cache.

Repository owner locked and limited conversation to collaborators Jun 27, 2024
@jpsca jpsca converted this issue into discussion #76 Jun 27, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants