Google app engine 如何呈现多个模板

Google app engine 如何呈现多个模板,google-app-engine,go,Google App Engine,Go,创建一个基本模板。 使用呈现的first.html,可以再创建一个模板 eg. : var tmpl = template.Must(template.ParseFiles( "templates/base.html", "templates/first.html", )) 但是我还想添加更多的.html文件来渲染。 任何引用?只需将.html文件作为参数添加,即可轻松添加更多.html文件: var tmpl = template.Must(template.

创建一个基本模板。 使用呈现的first.html,可以再创建一个模板

eg. :
    var tmpl = template.Must(template.ParseFiles(
    "templates/base.html",
    "templates/first.html",
    ))
但是我还想添加更多的.html文件来渲染。
任何引用?

只需将.html文件作为参数添加,即可轻松添加更多.html文件:

var tmpl = template.Must(template.ParseFiles(
    "templates/base.html",
    "templates/first.html",
    "templates/second.html",
))
只要第一个和第二个不定义相同的模板,这就可以了。
但是,模板包不允许使用模板名称的管道值动态调用模板。因此,如果您试图做一些类似于我下面的示例的事情,那么它将不起作用

存在一些变通办法,目前正在进行讨论。但模板包的设计似乎是让您每页都有一个
*模板
对象

尝试动态模板调用的失败示例:
错误:模板调用中出现意外“.Content”

主程序包
进口(
“日志”
“操作系统”
“文本/模板”
)
常数基=`
标题
{{template.Content}
`
const first=`{{define“first”}这是第一页{{end}`
const second=`{{define“second”}这里是第二个{{end}}`
类型视图结构{
内容字符串
}
func main(){
var view=&view{“first”}//这里我们尝试设置要作为内容查看的页面
t:=template.Must(template.New(“base”).Parse(base))
t=template.Must(t.Parse(first))
t=template.Must(t.Parse(秒))
err:=t.Execute(os.Stdout,view)
如果错误!=零{
Println(“正在执行的模板:”,错误)
}
}

如果您在模板文件夹中定义了所有模板,您可以使用以下工具轻松解析整个目录:

template.Must(template.ParseGlob("YOURDIRECTORY/*"))
例如:

head.html

{{define "header"}}
     <head>
         <title>Index</title>
     </head>
{{end}}

您可以使用
模板执行indexPage模板。ExecuteTemplate(w,“indexPage”,nil)
本教程的第3部分非常有用:

教程中的示例:

完整模板文件-t1.tmpl

{{define "t_ab"}}a b{{template "t_cd"}}e f {{end}}
{{define "t_cd"}} c d {{end}}
上面的文件将被解析为名为“t_ab”的模板。它里面有“a b/missing/e f”,但在字母表中缺少几个字母。为此,它打算包括另一个名为“t_cd”的模板(应该在同一组中)

完整模板文件-t2.tmpl

{{define "t_ab"}}a b{{template "t_cd"}}e f {{end}}
{{define "t_cd"}} c d {{end}}
上面的文件将被解析为一个名为“t_cd”的模板

完整程序

package main

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

func main() {
    fmt.Println("Load a set of templates with {{define}} clauses and execute:")
    s1, _ := template.ParseFiles("t1.tmpl", "t2.tmpl") //create a set of templates from many files.
    //Note that t1.tmpl is the file with contents "{{define "t_ab"}}a b{{template "t_cd"}}e f {{end}}"
    //Note that t2.tmpl is the file with contents "{{define "t_cd"}} c d {{end}}"


    s1.ExecuteTemplate(os.Stdout, "t_cd", nil) //just printing of c d
    fmt.Println()
    s1.ExecuteTemplate(os.Stdout, "t_ab", nil) //execute t_ab which will include t_cd
    fmt.Println()
    s1.Execute(os.Stdout, nil) //since templates in this data structure are named, there is no default template and so it prints nothing
}

如果要使用特定文件解析多个目录,请使用以下代码片段:

//parse a pattern in a specific directory  
allTemplates := template.Must(template.ParseGlob("Directory/*"))

//add another directory for parsing 
allTemplates = template.Must(allTemplates.ParseGlob("Another_Directory/*.tmpl"))

//add specific file name 
allTemplates = template.Must(allTemplates.ParseFiles("path/to/file.tmpl"))


func main() {
 ...
}

func FileHandler(w http.ResponseWriter, r *http.Request) {

    // access cached template by file name (in case you didn't define its name) 
    err := allTemplates.ExecuteTemplate(w, "file.tmpl", nil)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
}

func NameHandler(w http.ResponseWriter, r *http.Request) {

    // access cached template by handler name 
    err := allTemplates.ExecuteTemplate(w, "handlerName", nil)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
}
快捷方式:

allTemplates := template.Must(
        template.Must(
        template.Must(
            template.ParseGlob("directory/*.tmpl")).
            ParseGlob("another_directory/*.tmpl")).
            ParseFiles("path/to/file.tmpl")),
                )
file.tmpl
可以如下所示:

<html>
This is a template file 
</html>

安妮修斯:若我点击第一页按钮上的事件触发器,那个么它就会呈现前面提到的另一页。。。有重定向和renderTemplate函数,但它们也不起作用。对于base.html和first.html,我能够呈现页面。但对于第三个html页面,情况并非如此working@fmt.Fprint我认为您需要的是为模板和处理程序获取一些结构。凯尔·芬利对此有一个很好的建议:()这个链接已经尝试过了。base.html和first.html都可以。但是第三个和第四个的问题我对golang很陌生,但问题似乎归结为这样一个事实:您不能将string类型的变量传递给
模板
方法,而只能传递字符串文本?我也不是一个CS人,Go开发者有没有明显的理由做出这样的选择?我猜这实际上是在这里的文档中:
{{template“name”}指定名称的模板是用零数据执行的。
我只是不知道他们在写“name”表示字符串文字而不是写入
名称字符串
:main.go:var templates template.template templates=template.Must(template.ParseGlob(“templates/.tmpl”))func init(){http.HandleFunc(“/”,handler)}func handler(w http.ResponseWriter,r*http.Request){templates.ExecuteTemplate(w,“indexPage”,nil)}此示例给出错误“预期声明,找到'IDENT'模板”您的声明为false,它应该是:var templates:=template.Must(template.ParseGlob(“templates/.tmpl/*”))我假设您的所有模板都在templates/.tmpl/*目录中。我更新了此答案以更正类型和编译错误。+1因为这有助于我自己正确定义多个模板。起初并不清楚,因为我们大多数来自.net和其他具有布局和模板的语言,所以您不会创建一个主模板d一个单独的内容区域,但定义共享的页眉、页脚和侧栏。这实际上很好,因为您可以控制在每个模板流程上显示或不显示侧栏。这应该是公认的答案。这个答案比文档更清楚。它显示了如何使用全局
模板
变量,以便在server start上加载所有模板,而不是在实际的处理程序中加载(将在每个页面加载时加载)。它还显示了如何按名称定义模板,然后使用
模板调用它。ExecuteTemplate
,这非常有用。到目前为止,我在这个主题上看到的最好的文档…当您尝试添加第三个文件时出现了什么问题?您测试过吗?
语法错误:函数体外部的非声明语句
。@FisNan have您尝试改用
var allTemplates=…
。如果没有帮助,您仍然可以将所有代码放在
main()中
func,答案会告诉你一般的操作方法。希望我的评论能对你有所帮助,谢谢我使用了一个全局空指针
var allTemplates*template.template
,然后将所有
template.Must(…)
放入
main
函数中。谢谢你的回复!
<html>
This is a template file 
</html>
{{define "handlerName" }}
<p>this is a handler</p>
{{end}}