【问题标题】:How to concatenate a href in a go html template如何在go html模板中连接href
【发布时间】:2021-09-05 06:01:44
【问题描述】:

如果有人能帮我解决这个问题,我将不胜感激。我一直在经历类似的问题,但似乎没有一个对我的问题有帮助。

我在 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 连接起来的错误尝试。

【问题讨论】:

标签: go go-templates


【解决方案1】:

您不能使用+ 连接字符串。这应该有效:

<a href="http://localhost:8080/library/deleteuser/{{ .Id }}">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2018-12-07
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多