douli2063 2017-09-12 10:48
浏览 20
已采纳

如何用空模板覆盖模板块?

Using text/html I define a block in my base template, containing default content. In some situations I would like this block to be empty, so I thought I could just re-define its name and make it contain nothing like:

{{ block "something" . }}
    <h1>Default content</h1>
{{ end }}

// later in a place that does not want "something" ...
{{ define "something" }}{{ end }}

Somehow Go seems to think that this definition is "Zero" and will still render the default content unless I put any non-whitespace content into the definition.

I found this issue on the Golang repo which describes the very same thing nicely in a Playground example:

package main

import (
    "fmt"
    "os"
    "runtime"
    "text/template"
)

func main() {

    fmt.Printf("Version: %q
", runtime.Version())

    t, err := template.New("master").Parse(`{{block "area51" .}}Original Content{{end}}`)
    if err != nil {
        panic(err)
    }
    t, err = t.New("other_template").Parse(`{{define "area51"}}{{end}}`)
    if err != nil {
        panic(err)
    }

    fmt.Printf("Output:
")
    if err := t.ExecuteTemplate(os.Stdout, "master", nil); err != nil {
        panic(err)
    }

    fmt.Printf("
")

}

Weirdly, the issue mention it is fixed (and landed in 1.8.1 if I understand it correctly), but it does not work for me, neither with 1.8.1+ nor 1.9.

Is this a bug in Golang or is the approach flawed? Do I need to do anything differently in order to re-define the block so that it renders empty?

  • 写回答

1条回答 默认 最新

  • douhui1957 2017-09-12 11:41
    关注

    This is the expected behavior. This is documented at Template.Parse():

    Templates can be redefined in successive calls to Parse, before the first use of Execute on t or any associated template. A template definition with a body containing only white space and comments is considered empty and will not replace an existing template's body. This allows using Parse to add new named template definitions without overwriting the main template body.

    So you can't "erase" an already defined template (you can't replace its content to be empty).

    If you "conditionally" need it, then use an {{if}} action to decide if the template is to be called. Alternatively you may put an {{if}} inside the template, and the template itself may choose not to render anything. In this case you have to make sure to pass the proper argument that controls what the template will render.

    P.S. If you're working with HTML templates, you should always use html/template instead of text/template, as the former provides the same interface as package text/template but also provides contextual escaping to generate HTML output safe against code injection.

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

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3