I am ending up with an error: execute of template failed: template: partials/abc.html:27:28: executing "partials/abc.html" at <.context.myVar>: can't evaluate field myVarin type interface {}
With respect to #2, the dictionary you pass from baseof.html to list.html contains two unrelated objects, context and myVar. Then you pass a single object (the current context) from list.html to abc.html. Within abc.html, the context of list.html is accessed with .context, and the value of $myVar assigned in baseof.html is accessed with .myVar.
I had just started with it yesterday, so, there’s nothing much and most of the stuff is broken. I’m just getting started with Hugo, so, you might even notice a lot of weird code.
Getting back to my question, if you see my layouts/index.html, there’s a variable {{ $currentPage := .Permalink }}. I’m checking for strings in current URL to disable the buttons that lead there like this: <a {{ if not (in $currentPage $name) }} href = "{{ /tags/ }}{{ $name }}" {{ end }}>...</a>. I’ve done the same thing in layouts/partials/hero.html. So, instead of defining the variable multiple times, I was willing to know if there’s a way to define it once in layouts/_default/baseof.html and access it anywhere.
Can you please elaborate a little? From what I had read yesterday, .Scratch is used when we need to write to variables. How can that be used here and how’s using that better than a simple variable?
That’s some good guide. And I might have made some progress.
I added {{ .Scratch.Set "currentPage" .Permalink}} in my layouts/_default/baseof.html.
Then, I’m using {{ .Scratch.Get "currentPage"}} in my partial to get the value which is working great.
However, how do I use it in my conditions, for example this:
{{ if not (in $currentPage $name) }} (here, the $currentPage is the variable I have set before opening this topic)
If I change it to:
{{ if not (in {{ .Scratch.Get "currentPage"}} $name) }}
or
{{ if not (in .Scratch.Get "currentPage" $name) }}
It throws errors. Sure, I can store it as a variable again like {{ $abc := .Scratch.Get “currentPage” }} and that’s doing the work, but, it’s making no sense as I will again have to define the variable on all pages I need to use it.
Tbh, much here makes no sense to me. Tried to clone the repo to check for those errors, but no “luck”.
Also, since you’re trying to get the .Scratch inside a range, maybe you should be using something like: if not (in ($.Scratch.Get "currentPage") $name). That’s covered in the @regis guide, btw (good stuff).
{{ if not (in {{ .Scratch.Get "currentPage"}} $name) }}, I get the error: parse failed: template: index.html:36: unexpected "{" in operand
and if I use it like {{ if not (in .Scratch.Get "currentPage" $name) }}, I get the error: execute of template failed: template: index.html:36:20: executing "main" at <in>: wrong number of args for in: want 2 got 3.
EDIT: Sorry for the delay in replying, I was hit by daily limit of replying as a new user.
While I wait for the limit to be lifted, @sephore thank you for the answer. This works exactly as it should. Thanks to everyone for their time.