Golang模板:上下文中有什么内容?

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

Golang template: what is in the context?

问题

我想看一下传递到模板中的上下文中有什么内容。
我所做的就是在 HTML 中放置一个点:

{{$.}}

当渲染 HTML 时,我得到的结果是:

{http://localhost:1315/blog/index.xml 0xc08fdf36g0 map[Pages:[]] Blog [] map[] 2016-04-07 15:34:03 -0700 PDT 2016-04-07 15:34:03 -0700 PDT { 0} {/blog/ http://localhost:1315/blog/ } false 0xc0825878d0 {{0 0} 1} <nil>}

我知道在 . 中有 .Site,但它甚至在渲染的 HTML 中都没有显示出来。我如何以更易理解的形式获取上下文,以便在不预先知道它应该具有的所有属性的情况下查看它是什么?

英文:

I want to see what is in the context that is passed into the template.
What I do is basically putting the dot right there in the HTML.:

{{$.}}

What I get back when the HTML is rendered is:

{http://localhost:1315/blog/index.xml 0xc08fdf36g0 map[Pages:[]] Blog [] map[] 2016-04-07 15:34:03 -0700 PDT 2016-04-07 15:34:03 -0700 PDT { 0} {/blog/ http://localhost:1315/blog/ } false 0xc0825878d0 {{0 0} 1} <nil>}

I know for example there is .Site inside . but it doesn't even show up in the rendered HTML. How can I get the context in a more understandable form so I can see what it is without previously knowing all the attributes it is supposed to have?

答案1

得分: 5

应该打印出变量名和值。

英文:
{{ printf "%#v" . }}

Should print out the variable name and value.

huangapple
  • 本文由 发表于 2016年4月13日 10:55:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/36587727.html
匿名

发表评论

匿名网友

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

确定