[SOLVED] With used with variable

hi :slight_smile:

I am having hard time understanding the go template. Here is something I do not understand and I hope you can help me.

OK

{{ with .Params.m_faq1_10 }}
    {{ .title }} -> available
{{ end }}

OK

{{ $.Scratch.Get "ce" }} -> output ".Params.m_faq1_10"

BAD

{{ with $.Scratch.Get "ce" }}
    {{ .title }} -> NOT available
{{ end }}

BAD

{{ $a := $.Scratch.Get "ce" }}
{{ with $a }}
    {{ .title }} -> NOT available
{{ end }}

My question is how can I use $.Scratch.Get “ce” in “with”.

Anything inside a with “sees” only within the scope of the argument passed to with.

If with is passed .Site, .Title within that with would mean .Site.Title, and so on.

See this post by @regis for a better understanding of the scope/context, the dot, etc.

Sorry.
Seems like I choosed wrong example variable name becase .title has special meaning in hugo.

“.Params.m_faq1_10” has structure like:

m_faq1_10:
  classes_section: padding-right xs-hidden
  title: FAQ 1
  questions:
    - question: Lorem ipsum
      answer: >-
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Alias
        cumque earum facilis fugiat illo impedit in maxime minima, nisi quod.
        Beatae iure qui veritatis voluptate! Aliquam atque modi quae totam.</p>
    - question: Lorem ipsum dolor sit amet
      answer: >-
        <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consectetur
        adipisicing elit. Alias cumque earum facilis fugiat illo impedit in
        maxime minima, nisi quod. Beatae iure qui veritatis voluptate! Aliquam
        atque modi quae totam.</p>
    - question: Lorem ipsum
      answer: <p>Lorem ipsum dolor sit amet</p>
    - question: Lorem ipsum
      answer: <p>Lorem ipsum dolor sit amet</p>

Now if I am accessing by $.Scratch then .questions are not available:

{{ with $.Scratch.Get "ce" }}
    {{ .question }} -> NOT available
{{ end }}

and when accessing with .Params.m_faq1_10 everything is ok:

{{ with .Params.m_faq1_10 }}
    {{ .question }} -> IS available
{{ end }}

It’s not clear… where do you first .Set into the "ce" scratch? A lot of information is missing.

You can get better help if you share your site source.

It was my fault. So lame :slight_smile:

In ($.Scratch.Get “ce”) I stored string “.Params.something” instead of array .Params.something