模板引擎
Last updated
Was this helpful?
Was this helpful?
<h1>Hello {{.Name}}</h1>package main
import "gopkg.in/macaron.v1"
func main() {
m := macaron.Classic()
m.Use(macaron.Renderer())
m.Get("/", func(ctx *macaron.Context) {
ctx.Data["Name"] = "jeremy"
ctx.HTML(200, "hello") // 200 为响应码
})
m.Run()
}package main
import "gopkg.in/macaron.v1"
func main() {
m := macaron.Classic()
m.Use(macaron.Renderer(macaron.RenderOptions{
// 模板文件目录,默认为 "templates"
Directory: "templates",
// 模板文件后缀,默认为 [".tmpl", ".html"]
Extensions: []string{".tmpl", ".html"},
// 模板函数,默认为 []
Funcs: []template.FuncMap{map[string]interface{}{
"AppName": func() string {
return "Macaron"
},
"AppVer": func() string {
return "1.0.0"
},
}},
// 模板语法分隔符,默认为 ["{{", "}}"]
Delims: macaron.Delims{"{{", "}}"},
// 追加的 Content-Type 头信息,默认为 "UTF-8"
Charset: "UTF-8",
// 渲染具有缩进格式的 JSON,默认为不缩进
IndentJSON: true,
// 渲染具有缩进格式的 XML,默认为不缩进
IndentXML: true,
// 渲染具有前缀的 JSON,默认为无前缀
PrefixJSON: []byte("macaron"),
// 渲染具有前缀的 XML,默认为无前缀
PrefixXML: []byte("macaron"),
// 允许输出格式为 XHTML 而不是 HTML,默认为 "text/html"
HTMLContentType: "text/html",
}))
// ...
}main/
|__ main.go
|__ templates/
|__ hello.tmpl<h1>Hello {{Name}}</h1>package main
import (
"github.com/go-macaron/pongo2"
"gopkg.in/macaron.v1"
)
func main() {
m := macaron.Classic()
m.Use(pongo2.Pongoer())
m.Get("/", func(ctx *macaron.Context) {
ctx.Data["Name"] = "jeremy"
ctx.HTML(200, "hello") // 200 is the response code.
})
m.Run()
}package main
import (
"github.com/go-macaron/pongo2"
"gopkg.in/macaron.v1"
)
func main() {
m := macaron.Classic()
m.Use(pongo2.Pongoer(pongo2.Options{
// 模板文件目录,默认为 "templates"
Directory: "templates",
// 模板文件后缀,默认为 [".tmpl", ".html"]
Extensions: []string{".tmpl", ".html"},
// 追加的 Content-Type 头信息,默认为 "UTF-8"
Charset: "UTF-8",
// 渲染具有缩进格式的 JSON,默认为不缩进
IndentJSON: true,
// 渲染具有缩进格式的 XML,默认为不缩进
IndentXML: true,
// 允许输出格式为 XHTML 而不是 HTML,默认为 "text/html"
HTMLContentType: "text/html",
}))
// ...
}// ...
m.Use(macaron.Renderers(macaron.RenderOptions{
Directory: "templates/default",
}, "theme1:templates/theme1", "theme2:templates/theme2"))
m.Get("/foobar", func(ctx *macaron.Context) {
ctx.HTML(200, "hello")
})
m.Get("/foobar1", func(ctx *macaron.Context) {
ctx.HTMLSet(200, "theme1", "hello")
})
m.Get("/foobar2", func(ctx *macaron.Context) {
ctx.HTMLSet(200, "theme2", "hello")
})
// ...// ...
m.Use(pongo2.Pongoers(pongo2.Options{
Directory: "templates/default",
}, "theme1:templates/theme1", "theme2:templates/theme2"))
m.Get("/foobar", func(ctx *macaron.Context) {
ctx.HTML(200, "hello")
})
m.Get("/foobar1", func(ctx *macaron.Context) {
ctx.HTMLSet(200, "theme1", "hello")
})
m.Get("/foobar2", func(ctx *macaron.Context) {
ctx.HTMLSet(200, "theme2", "hello")
})
// ...// ...
m.Use(macaron.Renderers(RenderOptions{
Directory: "templates/default",
}, "templates/theme1", "templates/theme2"))
m.Get("/foobar", func(ctx *macaron.Context) {
ctx.HTML(200, "hello")
})
m.Get("/foobar1", func(ctx *macaron.Context) {
ctx.HTMLSet(200, "theme1", "hello")
})
m.Get("/foobar2", func(ctx *macaron.Context) {
ctx.HTMLSet(200, "theme2", "hello")
})
// ...// ...
m.Get("/foobar", func(ctx *macaron.Context) {
ok := ctx.HasTemplateSet("theme2")
// ...
})
// ...// ...
m.Get("/foobar", func(ctx *macaron.Context) {
ctx.SetTemplatePath("theme2", "templates/new/theme2")
// ...
})
// ...package main
import "gopkg.in/macaron.v1"
func main() {
m := macaron.Classic()
m.Use(macaron.Renderer())
m.Get("/", func(ctx *macaron.Context) {
ctx.Data["Name"] = "jeremy"
output, err := ctx.HTMLString("hello")
// 进行其它操作
})
m.Run()
}package main
import "gopkg.in/macaron.v1"
type Person struct {
Name string
Age int
Sex string
}
func main() {
m := macaron.Classic()
m.Use(macaron.Renderer())
m.Get("/xml", func(ctx *macaron.Context) {
p := Person{"Unknwon", 21, "male"}
ctx.XML(200, &p)
})
m.Get("/json", func(ctx *macaron.Context) {
p := Person{"Unknwon", 21, "male"}
ctx.JSON(200, &p)
})
m.Get("/raw", func(ctx *macaron.Context) {
ctx.RawData(200, []byte("raw data goes here"))
})
m.Get("/text", func(ctx *macaron.Context) {
ctx.PlainText(200, []byte("plain text goes here"))
})
m.Run()
}package main
import "gopkg.in/macaron.v1"
func main() {
m := macaron.Classic()
m.Use(macaron.Renderer())
m.Get("/status", func(ctx *macaron.Context) {
ctx.Status(403)
})
m.Get("/error", func(ctx *macaron.Context) {
ctx.Error(500, "Internal Server Error")
})
m.Get("/redirect", func(ctx *macaron.Context) {
ctx.Redirect("/") // 第二个参数为响应码,默认为 302
})
m.Run()
}main/
|__ main.go
|__ templates/
|__ hello.tmpl
|__ templates2/
|__ hello.tmpl<h1>Hello {{.Name}}</h1><h1>What's up, {{.Name}}</h1>package main
import "gopkg.in/macaron.v1"
func main() {
m := macaron.Classic()
m.Use(macaron.Renderer())
m.Get("/old", func(ctx *macaron.Context) {
ctx.Data["Name"] = "Unknwon"
ctx.HTML(200, "hello")
// 空字符串表示操作默认模板集
ctx.SetTemplatePath("", "templates2")
})
m.Get("/new", func(ctx *macaron.Context) {
ctx.Data["Name"] = "Unknwon"
ctx.HTML(200, "hello")
})
m.Run()
}