将两个范围合并以给出名称和超链接

huangapple go评论82阅读模式
英文:

Combine two ranges to give name and hyperlink

问题

我正在尝试创建一个与名称相关联的超链接,但是两个组分别位于两个不同的范围内,即名称位于一个范围内,链接位于另一个范围内。有没有办法将这两个组合起来?目前我拥有的HTML代码如下,但是我只想让名称包含超链接。

 <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}}

因此,我得到的输出是一个名称列表,然后是下面的链接列表。

如果有任何想法,将不胜感激。

谢谢

英文:

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.

 &lt;h5 class=&quot;card-title&quot;&gt;Members:&lt;/h5&gt;
   {{range .Members}}
   &lt;ul class=&quot;card-text&quot;&gt;
      &lt;li style=&quot;margin-bottom: 0rem;&quot;&gt;{{.}}&lt;/li&gt;
   &lt;./ul&gt;
   {{end}}
   &lt;h5 class=&quot;card-title&quot;&gt;WikiLink:&lt;/h5&gt;
     {{range .WikiLink}}
     &lt;ul class=&quot; card-text&quot;&gt;
       &lt;li style=&quot;margin-bottom: 0rem;&quot;&gt;&lt;a href=&quot;{{.}}&quot;&gt;{{.}}&lt;/a&gt;&lt;/li&gt;
     &lt;/ul&gt;
     {{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

答案1

得分: 1

你可以使用索引来迭代WikiLink数组在模板中:

<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}}

在Go Playground中的工作示例:https://go.dev/play/p/PKD-o3y09sk

英文:

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

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

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

huangapple
  • 本文由 发表于 2022年2月18日 21:12:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/71174052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定