How to order site params by source order?

Hi guys:

My site params is:

params:
  socials:
    GitHub: https://github.com/yourname || fab fa-github
    E-Mail: mailto:yourname@gmail.com || fa fa-envelope

And output in sidebar page like this:

{{ range $name,$val := $SP.socials }}
  {{ $href := "" }}
  {{ $icon := "" }}
  {{ range $i, $v := split $val "||" }}
    {{ if eq $i 0 }}
      {{ $href = $v }}
    {{ else }}
      {{ $icon = $v }}
    {{ end }}
  {{ end }}
  <span class="links-of-author-item">
    <a href="{{ $href }}" title="{{ $name }} " rel="noopener" target="_blank"><i class="{{ $icon }}"></i>{{ $name }}</a>
  </span>
{{ end }}

I was found the same required in this forum which link is https://discourse.gohugo.io/t/ordering-site-params-by-source-order/366/2 , but seems not good method to resolved it.

Does anyone had new idea, thanks.

By the way, is there had a function get array value by index not use range?

Maps are always sorted by key when iterating with a range loop:
https://discourse.gohugo.io/t/38500/2

I would restructure the data:

params:
  socials:
    - icon: fab fa-github
      name: GitHub
      url: 'https://github.com/yourname'
      weight: 1
    - icon: fa fa-envelope
      name: E-mail
      url: 'mailto:yourname@gmail.com'
      weight: 2

Then sort by weight:

{{ range (sort site.Params.socials "weight") }}
  <a href="{{ .url }}" title="{{ .name }}" rel="noopener" target="_blank"><i class="{{ .icon }}"></i>{{ .name }}</a>
{{ end }}

You can use the index function.

1 Like

Thanks for your reply. This way is nice and got it.

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