ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
File: view/template_html_2/main.go ~~~ package main import ( "github.com/kataras/iris" ) func main() { app := iris.New() tmpl := iris.HTML("./templates", ".html") tmpl.Layout("layouts/layout.html") tmpl.AddFunc("greet", func(s string) string { return "Greetings " + s + "!" }) app.RegisterView(tmpl) app.Get("/", func(ctx iris.Context) { if err := ctx.View("page1.html"); err != nil { ctx.StatusCode(iris.StatusInternalServerError) ctx.Writef(err.Error()) } }) // remove the layout for a specific route app.Get("/nolayout", func(ctx iris.Context) { ctx.ViewLayout(iris.NoLayout) if err := ctx.View("page1.html"); err != nil { ctx.StatusCode(iris.StatusInternalServerError) ctx.Writef(err.Error()) } }) // 设置派对的布局,。布局应该是任何Get或其他Handle派对的方法之前 my := app.Party("/my").Layout("layouts/mylayout.html") { // 这两个都将使用layouts / mylayout.html作为其布局. my.Get("/", func(ctx iris.Context) { ctx.View("page1.html") }) my.Get("/other", func(ctx iris.Context) { ctx.View("page1.html") }) } // http://localhost:8080 // http://localhost:8080/nolayout // http://localhost:8080/my // http://localhost:8080/my/other app.Run(iris.Addr(":8080")) } ~~~ File: view/template_html_2/templates/layouts/layout.html ~~~ <html> <head> <title>Layout</title> </head> <body> <h1>This is the global layout</h1> <br /> <!-- 在此处渲染当前模板 --> {{ yield }} </body> </html> ~~~ File: view/template_html_2/templates/layouts/mylayout.html ~~~ <html> <head> <title>my Layout</title> </head> <body> <h1>This is the layout for the /my/ and /my/other routes only</h1> <br /> <!-- Render the current template here --> {{ yield }} </body> </html> ~~~ File: view/template_html_2/templates/page1.html ~~~ <div style="background-color: black; color: blue"> <h1>Page 1 {{ greet "iris developer"}}</h1> {{ render "partials/page1_partial1.html"}} </div> ~~~ File: view/template_html_2/templates/partials/page1_partial1.html ~~~ <div style="background-color: white; color: red"> <h1>Page 1's Partial 1</h1> </div> ~~~