【问题标题】:Global template data全局模板数据
【发布时间】:2012-09-18 20:23:12
【问题描述】:

在执行ExecuteTemplate 时,我看到所有使用&whateversruct{Title: "title info", Body: "body info"} 将数据发送到模板以替换信息的示例。我想知道是否有可能不必在我的处理函数之外创建一个结构,因为我拥有的每个处理函数都不会有相同的标题、正文。能够向它发送替换模板信息的地图会很好。有什么想法或想法吗?

目前 - 写得很松散

type Info struct {
    Title string
    Body string
}

func View(w http.ResponseWriter) {
    temp.ExecuteTemplate(w, temp.Name(), &Info{Title: "title", Body: "body"})
}

似乎没有必要创建结构。您创建的每个函数的结构都不相同。所以你必须为每个函数(我知道的)创建一个结构。

【问题讨论】:

    标签: go go-templates


    【解决方案1】:

    补充凯文的回答:匿名结构将产生大致相同的行为:

    func View(w http.ResponseWriter) {
        data := struct {
            Title string
            Body  string
        } {
            "About page",
            "Body info",
        }
    
        temp.ExecuteTemplate(w, temp.Name(), &data)
    }
    

    【讨论】:

      【解决方案2】:

      那个结构只是一个例子。您也可以从外部传入结构,或者您可以按照您的建议使用地图。结构很好,因为结构的类型可以记录模板期望的字段,但这不是必需的。

      所有这些都应该工作:

      func View(w http.ResponseWriter, info Info) {
          temp.ExecuteTemplate(w, temp.Name(), &info)
      }
      

      func View(w http.ResponseWriter, info *Info) {
          temp.ExecuteTemplate(w, temp.Name(), info)
      }
      

      func View(w http.ResponseWriter, info map[string]interface{}) {
          temp.ExecuteTemplate(w, temp.Name(), info)
      }
      

      【讨论】:

        【解决方案3】:

        将模板应用于结构与地图之间的重要区别:使用地图,您可以在模板中进行地图中不存在的引用;模板将毫无错误地执行,并且引用将为空。如果您针对结构进行处理并引用结构中不存在的内容,则模板执行将返回错误。

        参考地图中不存在的项目可能很有用。考虑这个示例 web 应用程序中 views.go 中的 menu.html 和 getPage() 函数:https://bitbucket.org/jzs/sketchground/src。通过为菜单使用地图,可以轻松突出显示活动的菜单项。

        这种差异的简单说明:

        package main
        
        import (
            "fmt"
            "html/template"
            "os"
        )
        
        type Inventory struct {
            Material string
            Count    uint
        }
        
        func main() {
            // prep the template
            tmpl, err := template.New("test").Parse("{{.Count}} items are made of {{.Material}} - {{.Foo}}\n")
            if err != nil {
                panic(err)
            }
        
            // map first
            sweaterMap := map[string]string{"Count": "17", "Material": "wool"}
            err = tmpl.Execute(os.Stdout, sweaterMap)
            if err != nil {
                fmt.Println("Error!")
                fmt.Println(err)
            }
        
            // struct second
            sweaters := Inventory{"wool", 17}
            err = tmpl.Execute(os.Stdout, sweaters)
            if err != nil {
                fmt.Println("Error!")
                fmt.Println(err)
            }
        }
        

        【讨论】:

          【解决方案4】:

          你是完全正确的凯文!我更喜欢这个。谢谢!!!

          func View(w http.ResponseWriter) {
              info := make(map[string]string)
              info["Title"] = "About Page"
              info["Body"] = "Body Info"
              temp.ExecuteTemplate(w, temp.Name(), info)
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-03-02
            • 1970-01-01
            • 2011-12-17
            • 2011-01-14
            • 2014-01-08
            • 1970-01-01
            • 1970-01-01
            • 2023-04-07
            相关资源
            最近更新 更多