What's the equivalent for ordinal in templates?

I want to skip a class in the first image of a list page.

{{ range $k, $_ := .Pages }}
  {{ with .Resources.Get "cover.jpg" }}
    {{ if $k }}
      <img class="foo" src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
    {{ else }}
      <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
    {{ end }}
  {{ end }}
{{ end }}

$k is zero (falsy) for the first page.

1 Like

Is it possible to separate the image processing part from the rest of the code? I want to use it as a drop-in partial because I have about 8 templates using it.

{{ range $k, $_ := .Pages }}
  {{ partial "image.html" . }}
{{ end }}

layouts/partial/image.html

  {{ with .Resources.Get "cover.jpg" }}
    {{ if $k }}
      <img class="foo" src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
    {{ else }}
      <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
    {{ end }}
  {{ end }}

Sure. Pass $k along with .Page to the partial.

{{ partial "foo.html" (dict "page" . "index" $k) }}
1 Like

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