# Keeping a variable accessible through changes in context in an inline template

**URL:** <https://discourse.gohugo.io/t/keeping-a-variable-accessible-through-changes-in-context-in-an-inline-template/48651>\
**Category:** support\
**Created:** [March 4, 2024, 4:02pm UTC](https://discourse.gohugo.io/t/keeping-a-variable-accessible-through-changes-in-context-in-an-inline-template/48651 "2024-03-04T16:02:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Durand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tom_durand/32/14460_2.png) [@Tom\_Durand](https://discourse.gohugo.io/u/Tom_Durand)\
**Post date:** [March 4, 2024, 4:02pm UTC](https://discourse.gohugo.io/t/keeping-a-variable-accessible-through-changes-in-context-in-an-inline-template/48651/1 "2024-03-04T16:02:01Z")

</div>

Hi,  
I go through several loops in a menu.html, recursive calls to the same template to iterate through a filetree (yes, it works now). But then I need to keep track of the .Page I am into, and the $ does not do what I thought it should do. The value of $.Page **changes**.  
And then, .Store does the same.

```auto
{{ template "menu-filetree" (.GetPage "docs") }}
{{ define "menu-filetree" }}
  {{ $.Store.Get "currentpage" }}
<menu role="navigation" class=filetree>
{{ range .Pages }}
	<li>
		{{ if .IsSection }}
		<details class=ancestor open>
	    <summary role=button>
	    	{{- partial "docs/title-menu" .Page -}}
	    </summary>
	    {{ template "menu-filetree" . }}
		</details>
	 	{{ else }}
		<a href="{{ .Page.RelPermalink }}" aria-current=page>{{- partial "docs/title-menu" .Page -}}</a>
		{{end}}
	</li>
{{ end }}
</menu>
{{ end }}

```

In this case, here the value of `.Store "currentpage"` changes. How is this happening ? Isn’t this scratch variable local to the page ?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [March 4, 2024, 6:15pm UTC](https://discourse.gohugo.io/t/keeping-a-variable-accessible-through-changes-in-context-in-an-inline-template/48651/2 "2024-03-04T18:15:48Z")

</div>

> [@Tom\_Durand](#):
>
> the value of `.Store "currentpage"` changes

The $ is the context passed into the template.

You’ve defined a new template called “menu-filetree”.

The $ points to the context passed into “menu-filetree”.

If you want to access something from the template that first calls “menu-filetree”, you’ll have to pass it in context, both initially and with each recursive call.

---

<div class="post-metadata">

**Author:** ![Tom\_Durand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tom_durand/32/14460_2.png) [@Tom\_Durand](https://discourse.gohugo.io/u/Tom_Durand)\
**Post date:** [March 4, 2024, 10:39pm UTC](https://discourse.gohugo.io/t/keeping-a-variable-accessible-through-changes-in-context-in-an-inline-template/48651/3 "2024-03-04T22:39:02Z")

</div>

I can’t do that with a Scratch variable ?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [March 4, 2024, 10:44pm UTC](https://discourse.gohugo.io/t/keeping-a-variable-accessible-through-changes-in-context-in-an-inline-template/48651/4 "2024-03-04T22:44:45Z")

</div>

See my previous response. It depends on which page you want.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [March 4, 2024, 11:24pm UTC](https://discourse.gohugo.io/t/keeping-a-variable-accessible-through-changes-in-context-in-an-inline-template/48651/6 "2024-03-04T23:24:51Z")

</div>

Again, see my previous response.

> If you want to access something from the template that first calls “menu-filetree”, you’ll have to pass it in context, both initially and with each recursive call.

---

<div class="post-metadata">

**Author:** ![Tom\_Durand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tom_durand/32/14460_2.png) [@Tom\_Durand](https://discourse.gohugo.io/u/Tom_Durand)\
**Post date:** [March 4, 2024, 11:26pm UTC](https://discourse.gohugo.io/t/keeping-a-variable-accessible-through-changes-in-context-in-an-inline-template/48651/7 "2024-03-04T23:26:35Z")

</div>

Yes I read it, and it makes the code very cumbersome.  
So why can’t Scratch variables do the trick ?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [March 4, 2024, 11:32pm UTC](https://discourse.gohugo.io/t/keeping-a-variable-accessible-through-changes-in-context-in-an-inline-template/48651/8 "2024-03-04T23:32:35Z")

</div>

Because [scratch pads](https://gohugo.io/getting-started/glossary/#scratch-pad) are created and accessed via the `Scratch` and `Store` methods on a `Page` object. To get to the `Page` object you need to pass the `Page` object.

I’m not sure how to make this any clearer. Perhaps someone else can help.

---

<div class="post-metadata">

**Author:** ![Tom\_Durand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tom_durand/32/14460_2.png) [@Tom\_Durand](https://discourse.gohugo.io/u/Tom_Durand)\
**Post date:** [March 4, 2024, 11:42pm UTC](https://discourse.gohugo.io/t/keeping-a-variable-accessible-through-changes-in-context-in-an-inline-template/48651/9 "2024-03-04T23:42:33Z")

</div>

I got your point now.  
One question though, where can I find information on that `range $var := something` syntax ? I can’t understand it anymore. say that defines the context within the loop, then what is being iterated through exactly ?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [March 4, 2024, 11:45pm UTC](https://discourse.gohugo.io/t/keeping-a-variable-accessible-through-changes-in-context-in-an-inline-template/48651/10 "2024-03-04T23:45:01Z")

</div>

I would start by searching the documentation for the word range.

---

<div class="post-metadata">

**Author:** ![Tom\_Durand](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/tom_durand/32/14460_2.png) [@Tom\_Durand](https://discourse.gohugo.io/u/Tom_Durand)\
**Post date:** [March 5, 2024, 1:11am UTC](https://discourse.gohugo.io/t/keeping-a-variable-accessible-through-changes-in-context-in-an-inline-template/48651/11 "2024-03-05T01:11:23Z")

</div>

Ok, I could rewrite the original template (in term of functionality), understanding it this time. I suppose I should thank your RTFM stance, which at least for a programming language makes some sense. The user **can** make it right by trying, unlike with general OS problems.  
Still, sorry the doc is _very scant_, an exemple with “is rendered to:”. _That does not suffice_, more **words** in plain English, eventually with common cases it could help with, would be much appreciated. Even said tip was in the forum already… to find it, one must often _know what to look for_ first.

Here for instance I could not understand before trying out, that the context bound by a range does not mask variables defined earlier. Despite my own code using this elsewhere.  
Anyway.

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [March 7, 2024, 1:11am UTC](https://discourse.gohugo.io/t/keeping-a-variable-accessible-through-changes-in-context-in-an-inline-template/48651/12 "2024-03-07T01:11:24Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
