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

Feature Request: provide different data context for included template #221

Open
kiddliu opened this issue Nov 2, 2021 · 3 comments
Open

Comments

@kiddliu
Copy link

kiddliu commented Nov 2, 2021

Problem I'm Trying To Solve

I'd like to pass a variable as the data context of the included template, so a huge template can be split into more modularized sub-templates.

Current Solution

I can only have a huge template with complicated JSON as the only data context.

Requested Solution

It would be very convenient if I could have the following:

#include <string>

#include "data.h"
#include "util.h"

## for namespace in namespaces
    {{ include “namespace-template”:namespace }}
## endfor

in which "namespace-template" refers to the sub-template which handles the namespace only, and :namespace is the provided data context, so don't have to nest loops/conditions in a single template.

@Goutte
Copy link

Goutte commented Jun 11, 2022

I've found Twig to handle this in a very elegant and idiomatic manner:

Included templates have access to the variables of the active context.
If you are using the filesystem loader, the templates are looked for in the paths defined by it.
You can add additional variables by passing them after the with keyword:

{# template.html will have access to the variables from the current context and the additional ones provided #}
{% include 'template.html' with {'foo': 'bar'} %}

{% set vars = {'foo': 'bar'} %}
{% include 'template.html' with vars %}

You can disable access to the context by appending the only keyword:

{# only the foo variable will be accessible #}
{% include 'template.html' with {'foo': 'bar'} only %}

{# no variables will be accessible #}
{% include 'template.html' only %}

I don't know how doable it is, and it's perhaps best to stay close to Jinja. Just 2 cents, because the with/only pattern is really neat.

@kiddliu kiddliu closed this as completed Aug 14, 2022
@MaxandreOgeret
Copy link

MaxandreOgeret commented Nov 9, 2024

Hey @kiddliu & @pantor! How is that completed ? There's no attached PR and nothing in the doc. Please clarify, I believe this should be re-opened.
Indeed, how twig handles that is lovely

@kiddliu
Copy link
Author

kiddliu commented Nov 12, 2024

Hey @MaxandreOgeret, the reason why I closed this ticket is that my job no longer required a solution based on the template engine and from what I learned from the source code, it may require a lot of effort to bring in this context into inja. And eventually I was sticking with my ~300-line template and the performance of inja is bearable in my solution.

I agree with you that this is a nice-to-have feature for inja, so I'm reopening it...

@kiddliu kiddliu reopened this Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants