dswy34539 2013-07-14 07:07
浏览 243
已采纳

如何在Go中使用常量字符串变量中的嵌套模板?

I have been trying to use nested templates in Go, however the Examples or help documents are not helping me, and examples in 3 other blogs are not what I'm looking for (one is close and maybe the only way but I want to make sure)

OK, so my code is for App Engine, in here I will be doing an urlfetch to a server, and then I want to show some results, like the Response, headers, and body.

const statusTemplate = `
{{define "RT"}}
Status      - {{.Status}}
Proto       - {{.Proto}}
{{end}}
{{define "HT"}}
{{range $key, $val := .}}
{{$key}} - {{$val}}
{{end}}
{{end}}
{{define "BT"}}
{{.}}
{{end}}
{{define "MT"}}
<html>
    <body>
        <pre>
-- Response --
{{template "RT"}}

--  Header  --
{{template "HT"}}

--   Body   --
{{template "BT"}}
        </pre>
    </body>
</html>
{{end}}
{{template "MT"}}`

func showStatus(w http.ResponseWriterm r *http.Request) {
    /*
     *  code to get:
     *  resp as http.Response
     *  header as a map with the header values
     *  body as an string wit the contents
     */
    t := template.Must(template.New("status").Parse(statusTemplate)
    t.ExecuteTemplate(w, "RT", resp)
    t.ExecuteTemplate(w, "HT", header)
    t.ExecuteTemplate(w, "BT", body)
    t.Execute(w, nil)
}

My code actually outputs the RT, HT, and BT templates with the correct values, and then it outputs the MT template empty (MT stands for main template).

So... How can I use nested forms from a string variable so that an example as the above works?

  • 写回答

2条回答 默认 最新

  • dongzanxun2790 2013-07-14 20:17
    关注

    I think the approach you try with nested templates is wrong. If you want . to be defined inside a nested template, you have to supply an argument to the call to the nested template, just as you do with the ExecuteTemplate function:

    {{define "RT"}}
    Status      - {{.Status}}
    Proto       - {{.Proto}}
    {{end}}
    {{define "HT"}}
    {{range $key, $val := .}}
    {{$key}} - {{$val}}
    {{end}}
    {{end}}
    {{define "BT"}}
    {{.}}
    {{end}}
    {{define "MT"}}
    <html>
        <body>
            <pre>
    -- Response --
    {{template "RT" .Resp}}
    
    --  Header  --
    {{template "HT" .Header}}
    
    --   Body   --
    {{template "BT" .Body}}
            </pre>
        </body>
    </html>
    {{end}}
    {{template "MT"}}
    

    The important part you seem to miss is that templates do not encapsulate state. When you execute a template, the engine evaluates the template for the given argument and then writes out the generated text. It does not save the argument or anything that was generated for future invocations.

    Documentation

    Relevant part of the documentation:

    Actions

    Here is the list of actions. "Arguments" and "pipelines" are evaluations of data, defined in detail below.

    ...
    
    {{template "name"}}
      The template with the specified name is executed with nil data.
    
    {{template "name" pipeline}}
      The template with the specified name is executed with dot set
      to the value of the pipeline.
    
    ...
    

    I hope this clears up things.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体