Display []string slice as a vertical list in HTML template

So I have this slice

UserIDs: []string{"A1", "A2", "A3", "B1", "B2", "C1", "C2", "C3", "C4"}

And I have this table in my HTML template

<table>
	<tr>
		<td>User role: </td>
	    <td><span class="italic alignright">{{.ObjectMap.UserRole}}</span></td>
    </tr>
    <tr>
	    <td>User ID's: </td>
	    {{range $k, $v := .UserIDs}}
	    <td><span class="italic alignright">{{.}}</span></td>
		{{end}}
    </tr>
</table>

I would like to print “User ID’s” in one column and then in the column adjacent I’d like the list:

A1
A2
A3
B1
B2
C1
C2
C3
C4

Also the cell size must change depending on the number of User ID’s.
How do I go about this, I was thinking inside the range function but I have no Idea where to start, thanks.

Ok, 3 hours later I have the solution xD

<table>
	<tr>
		<td>User role: </td>
	    <td align="right">{{.ObjectMap.UserRole}}</td>
    </tr>
    <tr style="vertical-align: top;">
	    <td>User ID's: </td>
	    <td align="right">{{range $i := .UserIDs}}{{$i}}<br/>{{end}}</td>
    </tr>
</table>

Hopefully others will find this useful.

1 Like

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