Get variable outside the range

Hello,

i’m trying to get the variable $pix_counter outside the range {{ range .Params.photo }}.
The objective is to change the thumbnailWidth in relation to the number of photos in my .Params.photo

I have read the docs over and over and dozens of topics on the forum (i’m french so i may have missed something). I understood that the key is the .Scratch thing i have try many things : .Scratch.Set, .Scratch.Get,.Scratch.Add

But nothing works, i’m going to be crazy.
If someone can help me to figured this out that would be really kind :slight_smile:
Here is my partial code source

  <div class="micro-box">
	{{ if isset .Params "photo" }}
  {{ $pix_counter := 0 }}
  {{ range .Params.photo }}
    {{ $pix_counter = add $pix_counter 1 }}
        {{/* The .Scratch.Set stuff must be here i presume */}}
      {{ end }}
  	<div id="nanogallery_{{ with .File }}{{ .ContentBaseName }}{{ end }}" data-nanogallery2='{
      {{/* And the .Scratch.Get stuff must be here i presume */}}t
      {{ if $pix_counter eq 1 }}
	    "thumbnailWidth": "800",
      {{ else if $pix_counter eq 2 }}
	    "thumbnailWidth": "400",
      {{ else if $pix_counter ge 3 }}
	    "thumbnailWidth": "200",
      {{ end }}
	    "thumbnailHeight": "auto",
	    "locationHash": "true",
	    "allowHTMLinData": "true"
	}'>
	{{ range .Params.photo }}
	  {{ $media_base_url := "https://media.jeer.fr/" }}
	  {{ $media_filename := strings.TrimPrefix $media_base_url . }}
	<a href="{{ . }}" class="u-photo">{{ $my_title }}</a>
	{{ end }}
	</div>
  {{ end }}
</div>

Thank You

Hi,

I’m not sure I understand. What are you trying to do with the .Scratch?

Hi @pointyfar thank for your reply

i’m trying to get the variable $pix_counter outside the range {{ range .Params.photo }}

because when i call the variable $pix_counter outside the range without Scratch hugo say me that $pix_counter is not set.

These should be

{{ if eq $pix_counter 1 }}

etc…

Doc: compare.Eq | Hugo

You don’t need .Scratch.

2 Likes