Home
Support Forums

Using range to generate a list from multiple fields of data?

Hello,

I am a complete Hugo/Netlify noob, but diving in feet first. Thank you in advance for any assistance.

I have the CMS set up with the following collections:

  - name: "artists" 
    label: "Artists"
    folder: "site/content/artists" 
    create: true 
    fields: 
      - {label: "Artist name", name: "title", widget: "string"}
      - {label: "Main Image", name: "main_image", widget: "image"}
      - {label: "Image Gallery", name: "image_gallery", widget: "image"}
      - {label: "Biography", name: "description", widget: "text"}
  - name: "exhibitions"
    label: "Exhibitions"
    folder: "site/content/exhibitions"
    create: true
    fields:
      - {label: "Exhibition title", name: "title", widget: "string"}
      - {label: "Start date", name: "start_date", widget: "date"}
      - {label: "End date", name: "end_date", widget: "date"}
      - {label: "Gallery artist(s)", name: "gallery_artists", widget: "relation", collection: "artists", search_fields: ["title"], value_field: "title", multiple: true, required: false}
      - {label: "Other Artist(s)", name: "other_artists", widget: "list", summary: '{{fields.other_artist_name}}', field: 
          {label: "Artist name", name: "other_artist_name", widget: "string", required: false}}

And right now the layout code is the following:

      	<ul>
        {{ range .Params.gallery_artists }}
      		<li>{{ . }}</li>
      	{{ end }}
        {{ range .Params.other_artists }}
          <li>{{ . }}</li>
        {{ end }}
      	</ul>

The ultimate goal is to sort them all together alphabetically.

So I suppose I’m hoping for either a way to get the “gallery_artists” and “other_artists” together (via range?), rather than with the two separate range functions.

Or a way to sort them after they’ve been retrieved by the separate range functions?

Thank you again.

Hi @curtiswallen,

This question is more suited for Hugo Forums, as you’ve more chances or receiving a (better) answer there. However, I think you can use the sort function of Hugo:

<ul>
  {{ range sort .Params.other_artists }}
    <li>{{ . }}</li>
  {{ end }}
</ul>

Reference: