# Question: Importance Of Tera Variable Assignment Location (Not Loop Related)

**URL:** <https://zola.discourse.group/t/question-importance-of-tera-variable-assignment-location-not-loop-related/2525>\
**Category:** Support\
**Created:** [March 15, 2025, 7:19pm UTC](https://zola.discourse.group/t/question-importance-of-tera-variable-assignment-location-not-loop-related/2525 "2025-03-15T19:19:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raymi306](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/raymi306/32/1431_2.png) [@Raymi306](https://zola.discourse.group/u/Raymi306)\
**Post date:** [March 15, 2025, 7:19pm UTC](https://zola.discourse.group/t/question-importance-of-tera-variable-assignment-location-not-loop-related/2525/1 "2025-03-15T19:19:55Z")

</div>

Please note I am behind on zola versions, mine is 0.18.0

Reproduction:

1. `zola init`
2. create content/\_index.md
3. create templates/base.html

```html
<!doctype html>
<html>
	<body>
		{% block body %}{% endblock %}
	</body>
</html>

```

1. create failing templates/index.html

```auto
{% extends "base.html" %}

{% if section %}
    {% set this = section %}
{% else %}
    {% set this = page %}
{% endif %}

{% block body %}
{{ this.content | safe }}
{% endblock %}

```

1. `zola serve` fails:

```auto
Error: Failed to build the site
Error: Failed to render section '/home/andrew/projects/tera-bug-poc/content/_index.md'
Error: Reason: Failed to render 'index.html'
Error: Reason: Variable `this.content` not found in context while rendering 'index.html'

```

1. Place if…else inside any block (this example only has a body)
2. `zola serve` succeeds

I think I am just not understanding something about how Tera works here and was hoping someone could explain why “7” works. I thought variable scope might be limited to blocks, but when I have multiple blocks, I found that I could place the assignment in multiple positions and it would work, for instance, place the variable assignment in “meta” block but successfully access the variable in “body” block.

---

<div class="post-metadata">

**Author:** ![keats](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@keats](https://zola.discourse.group/u/keats)\
**Post date:** [March 15, 2025, 9:02pm UTC](https://zola.discourse.group/t/question-importance-of-tera-variable-assignment-location-not-loop-related/2525/2 "2025-03-15T21:02:32Z")

</div>

```auto
{% extends "base.html" %}

{% if section %}
    {% set this = section %}
{% else %}
    {% set this = page %}
{% endif %}

{% block body %}
{{ this.content | safe }}
{% endblock %}

```

When you are writing templates that are extending other templates any code outside of blocks is ignored.

You have to do:

```auto
{% extends "base.html" %}

{% block body %}

{% if section %}
    {% set this = section %}
{% else %}
    {% set this = page %}
{% endif %}

{{ this.content | safe }}
{% endblock %}

```

```auto

```

---

<div class="post-metadata">

**Author:** ![Raymi306](https://yyz2.discourse-cdn.com/free1/user_avatar/zola.discourse.group/raymi306/32/1431_2.png) [@Raymi306](https://zola.discourse.group/u/Raymi306)\
**Post date:** [March 15, 2025, 9:21pm UTC](https://zola.discourse.group/t/question-importance-of-tera-variable-assignment-location-not-loop-related/2525/3 "2025-03-15T21:21:05Z")

</div>

Thank you, yes that was what I meant for step 7.

I do not see this behavior mentioned in the docs.  
If you would like, I can make a PR.

---

<div class="post-metadata">

**Author:** ![keats](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@keats](https://zola.discourse.group/u/keats)\
**Post date:** [March 17, 2025, 10:03am UTC](https://zola.discourse.group/t/question-importance-of-tera-variable-assignment-location-not-loop-related/2525/4 "2025-03-17T10:03:22Z")

</div>

I’ll take a PR. It’s kind of not mentioned well either in Tera docs: [Tera](https://keats.github.io/tera/docs/#child-template)
