Need page variable value inside hugo range

So, I have a range

{{ range .Params.gallery }} <img src="{{ . | relURL }}"> {{.Content}} {{ end }}

Is it possible to get the page variable value inside range loop?

Or I can store the content value and render in the range loop?

But what is .Params.gallery here exactly? It would be easier to me to understand.

If anything, could this be useful? 1. Define a Variable Independent of Context

I have

gallery:
    - image : "images/image.png" 
    - image : "images/image.png"

in my content/page.md page as those variable as front matter.

I tried these, just working fine with site variable but not print the page variable value.

What about:

{{ range .Page.Params.gallery }} {{ with $.Page.Resources.GetMatch .image }} <img src="{{ .RelPermalink }}"> {{ .Name }} {{ end }} {{ end }}

Forgot to ask if it was a shortcode or not… And file structure… (the example above would be a shortcode and page resources)

So the file struture is like this

/layouts/_default/single.html
    {{ range .Params.gallery }} 
    <img src="{{ . | relURL }}"> 
    	{{.Content}} 
    {{ end }}

The content page

/content/page.md
# has front matter as 
---
title: "Page"
gallery:
  - image : "images/image.png" 
  - image : "images/image.png"
---
Context text are here

With the above code, I am able to get the gallery params image path. But the {{.Content}} is empty.

AaAaAaAand I also forgot to ask if the images are in the static or assets folder… sorry.

Cuz if you’re using the static folder, I don’t think something like .Content should work at all?

WAIT… Wait, with .Content you just want to print the path? If so (now assuming files are in static folder):

{{ range .Params.gallery }}  {{ with .image }}<img src="{{ . | relURL }}">  {{ . }} {{ end }} {{ end }}

Images are in static/images path

with .Content I am getting empty value

No, I want the content from the markdown page

… The said /content/page.md?

Yes right

Hmmm… Right. I didn’t understand before cuz, by doing that you would be basically repeating the page content after inserting each image (and why would someone want to do that?). Also, you said that you tried defining a variable and it didn’t work… but that’s still my solution.

{{ $content := .Content }}
{{ range .Params.gallery }} <img src="{{ . | relURL }}"> {{ $content }} {{ end }}

Seriously, this was really confusing. If I still didn’t get it right, I’m not willing to keep on trying. Sorry.

1 Like

This works !!! Thank you.

1 Like

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