Write a hugo template code which can list all elements except the first elements

---
title: Dictionary Indexing
xdescription: 
- AA
- BB
- CC
- DD
---

I am tring to show all of them except the first one as list. I wrote the following code to do but it is not displaying items. Any idea how to display all elements except the first one?

Below is the code I tried:

    {{ with .Params.xdescription}}
    <h2>Description___</h2>
    {{ range $i, $myElement := . }}
    {{ if gt $i 1 }}
    {{ $i }}
    {{ $myElement}}
    {{ end }}
    {{ end }}
    {{ end }}

The correct value to access this configuration is .Site.Params.xdescription. .Params is to access the front matter of the current page.

Also, the first element of a slice is 0, not 1 so you may want to change that if check to if gt $i 0.

A better way to achieve this would be to create a slice where the first element is removed and you just loop over it. This can be implemented as follows:

{{ with .Site.Params.xdescription}}
  <h2>Description___</h2>
  {{ $list := last (sub (len .) 1) . }}
  {{ range $list }}
    {{ . }}
  {{ end }}
{{ end }}

HTH

{{ with .Params.xdescription }}
  <h2>Description</h2>
  {{ range $k, $v := . }}
    {{ if $k }}{{ $v }}{{ end }}
  {{ end }}
{{ end }}

$k is 0 (falsy) on the first iteration, so it is skipped.

1 Like

You also should be able to use “range after 1”
after | Hugo (gohugo.io)

2 Likes

Thanks all for your solution. @andrewd72 's solution is more cleaner. Here is the solution I implemented:

        {{ with .Params.xdescription}}
        <h2>Description__Method 1 _</h2>
        <ul>
            {{ range $i, $myElement := . }}
            {{ if gt $i 0 }}
            <li>
                {{ $myElement}}
                {{ end }}
            </li>
            {{ end }}
        </ul>
        {{ end }}

        {{ with .Params.xdescription}}
        <h2>Description_Method 2</h2>
        <ul>
            {{ range after 1 . }}
            <li>
                {{ . }}
                {{ end }}
            </li>
        </ul>
        {{ end }}

And out put looks like the below:
Screenshot 2023-01-15 at 9.50.06 AM

Actually I used method 2 for the implementation. Thanks

1 Like

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