1

I am trying to create a hyperlink attached to a name, however both groups are in two separate ranges i.e. names are in one range and the links are in another. Is there a way to combine the two? Currently the html code I have is as below, however, I want only the name to contain the hyperlink.

 <h5 class="card-title">Members:</h5>
   {{range .Members}}
   <ul class="card-text">
      <li style="margin-bottom: 0rem;">{{.}}</li>
   <./ul>
   {{end}}
   <h5 class="card-title">WikiLink:</h5>
     {{range .WikiLink}}
     <ul class=" card-text">
       <li style="margin-bottom: 0rem;"><a href="{{.}}">{{.}}</a></li>
     </ul>
     {{end}}

The output I am therefore getting is a list of names and then a list of links below.

Any ideas would be appreciated.

Thanks

kostix
  • 51,517
  • 14
  • 93
  • 176
Fazila
  • 55
  • 5
  • What is this syntax? is it `Go`, `HTML`? Are you trying to write `Go` code inside of `HTML` file? – rosmak Feb 18 '22 at 13:30
  • @rosmak, yes, it's highly Go-specific. The syntax is [here](https://pkg.go.dev/html/template) in case you're curious ;-) I've retagged the question properly. – kostix Feb 18 '22 at 13:33
  • 1
    Please refer to https://stackoverflow.com/help/minimal-reproducible-example – rosmak Feb 18 '22 at 13:38
  • I'm sorry I'm pretty new to this I am creating a web page using Go and HTML? the code above is from a HTML file but the ranges are from Go - sorry if I'm not being very clear not sure how to explain it. – Fazila Feb 18 '22 at 13:50

1 Answers1

1

You can iterate over WikiLink array by index in the template:

 <h5 class="card-title">Members:</h5>
   {{range $i, $m :=.Members}}
   <ul class="card-text">
      <li style="margin-bottom: 0rem;"><a href="{{index $.WikiLink $i}}">{{$m}}</a></li>
   </ul>
 {{end}}

Working example in Go Playground: https://go.dev/play/p/PKD-o3y09sk

Danil Perestoronin
  • 1,063
  • 1
  • 7
  • 9