Partial add additional variable

Hello. Hugo is great, but I think it’s horrible documentation for passing variables make it hard to use for newbies liek me.

I have partial

In homepage I use

 {{ $pages := where .Site.RegularPages "Type" "items" }}
 {{ $pages = $pages | first 3 }}
 {{ range $pages }}
 {{ partial "grid-single.html" . }}
 {{ end }}

but I want to pass also color. For example $color = “blue”, so my grid will be blue, etc.

Also inside grid-single. I want $color = %color passed% but by default black (if not passed)

Please halp

Use a dict instead of dot

{{ partial "grid" (dict "color" "blue" "context" .) }}

and access it in your partial

{{ $color := .color }}
{{ $context := .context }}

Use $context instead of dot in your partial then. And read up about dot and context. There are other, better sources than the documentation.

I used other method useing sctach.set, scratch.get and scratch.delete, but thanks

While that’s possible, it’s awkward and not how partials are parametrized usually.

for basic partials it is good, but when all your partials assume they have context in . it is weird to have one partial in one style, other - in other

besides my skills are very low and for that partial it didn’t work

not all partials are created equal. But suit yourself.

I’m sorry you think it is horrible. This page…

… includes 4 examples of passing values:

  1. Passing a slice
  2. Passing a page
  3. Passing nothing
  4. Passing a map

I’m not sure how to make it any clearer.

One additional example adding dot to a dict might be beneficial.



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