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

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.

{{ 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 ?

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.

I can’t do that with a Scratch variable ?

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

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.

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

Because scratch pads 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.

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 ?

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

1 Like

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.

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