doujiang1939 2014-10-28 20:20
浏览 8
已采纳

访问模板中包含空格的字段

I am trying to figure out how I can access a map field in a template that contains a space in Go. But I can't seem to figure this out..

I have decoded a JSON array from another source that I don't control like so:

var f []interface{}
json.Unmarshal(externalData, &f)

Then I pass it to ExecuteTemplate as follows:

templates.ExecuteTemplate(w, "templates/data.html", map[string]interface{} { "Data": f })

In my template I use this:

<table>
{{range $element := .Data}}
<tr><td>{{$element.Name}}</td><td>{{$element.**Some Field With Spaces**}}</tr>
{{end}}
</table>

And that is where it goes wrong. I guess I can solve this by renaming the "Some Field With Spaces" key manually before sending the data to the template but I'd like to keep it as is. I figure I could probably also make a template function that returns the right key name but that doesn't seem like a very good solution.

  • 写回答

1条回答 默认 最新

  • dsbruqxgt820011351 2014-10-28 20:55
    关注
    {{index $element "some field with spaces"}}
    

    playground example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi