Access variable from baseof in partial


I’m trying to access a variable that I created in layouts/_default/baseof.html inside layouts/partials/abc.html.

Currently, my structure is something like the following…


    {{ $myVar:= "value" }}
    {{ block main (dict "context" . "myVar" $myVar) }}
    {{ end }}


{{ define "main" }}
  {{ partial "abc" . }}
{{ end }}


 {{  .context.myVar }}

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

better place ist your config file.
define your value in [Params]

Would it work for dynamically set variables values like .Permalink?

Nope, all variables in baseof or config are static.

For more help share a (sample) repository and tell us, what you want to do.

Back to your original post, you have two problems:

  1. In baseof.html, change:

    {{ block main (dict "context" . "myVar" $myVar) }}


    {{ block "main" (dict "context" . "myVar" $myVar) }}
  2. In abc.html, change:

    {{ .context.myVar }}


    {{ .myVar }}

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.

So here’s the current repo of my website that’s working:

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.

By any chance, are you aware of a documentation page, blog post or tutorial that explains this in more detail?

I’m afraid that if you keep going down this road, you’ll need to learn about .Scratch at some point.

1 Like

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?

1 Like

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) }}


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

Such as…

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).

1 Like

If I use it like:

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

Never nest brackets. No:

{{ if not (in {{ .Scratch.Get "currentPage"}} $name) }}

wrong number of args for in: want 2 got 3

This tells you exactly what you did wrong. Don’t give up because of an error. Learn from it.


I’ll keep that in mind. Thank you.

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