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

Passing a variable into the parent template #692

Open
jedenastka opened this issue Nov 24, 2021 · 1 comment
Open

Passing a variable into the parent template #692

jedenastka opened this issue Nov 24, 2021 · 1 comment

Comments

@jedenastka
Copy link

I cannot pass a variable from a child template to it's parent.

I have this in child:

{% extends "parent" %}
{% set foo = "bar" %}

{% block content %}{% endblock content %}

And I'm trying to use the foo variable from parent:

[...]
{{ foo }}

{% block content %}{% endblock content %}
[...]

When I try to render child, I get an error saying foo isn't in scope:

Variable `foo` not found in context while rendering 'child'

Jinja appearently can do this: https://jinja.palletsprojects.com/en/3.0.x/tricks/#highlighting-active-menu-items

Note: I'm using Tera with Rocket.

@Keats
Copy link
Owner

Keats commented Nov 24, 2021

In Tera, template outside of blocks is completely ignored in children. The way to do it here is to define a block in your parent template and override it in the children. Eg

{% block extra_data %}
// In tera unknown values are an error so it should be set to something if it's possible that it never gets set in a children.
{% set foo = "" %}
{% endblock %}

... and in the children
{% block extra_data %}
{% set foo = "bar" %}
{% endblock %}

should work and you should be able to use {{foo}} after the extra_data block

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

No branches or pull requests

2 participants