How to concatenate a href in a go html template

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

How to concatenate a href in a go html template

问题

我很乐意帮助你解决这个问题。我一直在查看类似的问题,但似乎没有一个能解决我的问题。

我在go模板中有一个用户表格。在最右边的列中,我有两个按钮,用于更新和删除用户。我希望这些按钮指向一个URL,以便更新和删除特定的用户。因此,我需要在URL中包含用户ID。

以下是代码:

<table class="table table-striped">
    <thead>
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Date Of Birth</th>
            <th>County</th>
            <th>Books</th>
            <th>Perform Action</th>
        </tr>
    </thead>
    <tbody>
        {{ range .Users}}
        <tr>
            <td>{{ .Id }}</td>
            <td>{{ .Name }}</td>
            <td>{{ .DateOfBirth }}</td>
            <td>{{ .County }}</td>
            <td>
                {{if .Books}}
                {{ range $id, $book := .Books }}
                <li>{{ $book.Title }}</li>
                {{ end }}
                {{else}}
                No Books
                {{end}}
            </td>
            {{ $updateUrl := "http://localhost:8080/library/updateuser/" + .Id }}
            <td><a href="http://localhost:8080/library/deleteuser/"+{{ .Id }} class="btn btn-outline-dark ml-3">Update User</a><a href="http://localhost:8080/library/deleteuser/"+{{ .Id }} class="btn btn-outline-dark ml-3">Delete User</a></td>
        </tr>
        {{ end}}
    </tbody>
</table>

在倒数第5行,你会看到我错误地尝试将href与id连接起来。

英文:

I'd really appreciate if somebody could help me out with this. I've been going through similar questions asked but none seem to help with my problem.

I have a User table in a go template. In the right most column I have two buttons to update and delete a user. I want these buttons to point to a url that will update and delete that specific user. So I need to have the user id in the url.

This is the code:

&lt;table class=&quot;table table-striped&quot;&gt;
    &lt;thead&gt;
      &lt;tr&gt;
        &lt;th&gt;Id&lt;/th&gt;
        &lt;th&gt;Name&lt;/th&gt;
        &lt;th&gt;Date Of Birth&lt;/th&gt;
        &lt;th&gt;County&lt;/th&gt;
        &lt;th&gt;Books&lt;/th&gt;
        &lt;th&gt;Perform Action&lt;/th&gt;
      &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
      {{ range .Users}}
      &lt;tr&gt;
          &lt;td&gt;{{ .Id }}&lt;/td&gt;
          &lt;td&gt;{{ .Name }}&lt;/td&gt;
          &lt;td&gt;{{ .DateOfBirth }}&lt;/td&gt;
          &lt;td&gt;{{ .County }}&lt;/td&gt;
          &lt;td&gt;
            {{if .Books}}
              {{ range $id, $book := .Books }}
                &lt;li&gt;{{ $book.Title }}&lt;/li&gt;
              {{ end }}
            {{else}}
            No Books
            {{end}}
          &lt;/td&gt;
          {{ $updateUrl := &quot;http://localhost:8080/library/updateuser/&quot; + .Id }}
          &lt;td&gt;&lt;a href=&quot;http://localhost:8080/library/deleteuser/&quot;+{{ .Id }} class=&quot;btn btn-outline-dark ml-3&quot;&gt;Update User&lt;/a&gt;&lt;a href=&quot;http://localhost:8080/library/deleteuser/&quot;+{{ .Id }} class=&quot;btn btn-outline-dark ml-3&quot;&gt;Delete User&lt;/a&gt;&lt;/td&gt;
      &lt;/tr&gt;
      {{ end}}
    &lt;/tbody&gt;
  &lt;/table&gt;

In the 5th line up from the bottom you'll see my incorrect attempt at concatenating the href with the id.

答案1

得分: 3

你不能使用+来连接字符串。应该使用以下方式:

&lt;a href=&quot;http://localhost:8080/library/deleteuser/{{ .Id }}&quot;&gt;
英文:

You cannot concatenate strings using +. This should work:

&lt;a href=&quot;http://localhost:8080/library/deleteuser/{{ .Id }}&quot;&gt;

huangapple
  • 本文由 发表于 2021年6月21日 22:48:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/68070058.html
匿名

发表评论

匿名网友

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

确定