mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
Add a 'ParseTemplate' method on view engines to manually parse and add a template from a text
examples at: https://github.com/kataras/iris/tree/master/_examples/view/parse-template relative to: https://github.com/kataras/iris/issues/1617
This commit is contained in:
28
_examples/view/parse-template/amber/main.go
Normal file
28
_examples/view/parse-template/amber/main.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris/v12"
|
||||
|
||||
func main() {
|
||||
e := iris.Amber(nil, ".amber") // You can still use a file system though.
|
||||
e.AddFunc("greet", func(name string) string {
|
||||
return "Hello, " + name + "!"
|
||||
})
|
||||
err := e.ParseTemplate("program.amber", []byte(`h1 #{ greet(Name) }`))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
app := iris.New()
|
||||
app.RegisterView(e)
|
||||
app.Get("/", index)
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func index(ctx iris.Context) {
|
||||
ctx.View("program.amber", iris.Map{
|
||||
"Name": "Gerasimos",
|
||||
// Or per template:
|
||||
// "greet": func(....)
|
||||
})
|
||||
}
|
||||
26
_examples/view/parse-template/django/main.go
Normal file
26
_examples/view/parse-template/django/main.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris/v12"
|
||||
|
||||
func main() {
|
||||
e := iris.Django(nil, ".html") // You can still use a file system though.
|
||||
e.AddFunc("greet", func(name string) string {
|
||||
return "Hello, " + name + "!"
|
||||
})
|
||||
err := e.ParseTemplate("program.html", []byte(`<h1>{{greet(Name)}}</h1>`))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
app := iris.New()
|
||||
app.RegisterView(e)
|
||||
app.Get("/", index)
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func index(ctx iris.Context) {
|
||||
ctx.View("program.html", iris.Map{
|
||||
"Name": "Gerasimos",
|
||||
})
|
||||
}
|
||||
24
_examples/view/parse-template/handlebars/main.go
Normal file
24
_examples/view/parse-template/handlebars/main.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris/v12"
|
||||
|
||||
func main() {
|
||||
e := iris.Handlebars(nil, ".html") // You can still use a file system though.
|
||||
e.ParseTemplate("program.html", `<h1>{{greet Name}}</h1>`, iris.Map{
|
||||
"greet": func(name string) string {
|
||||
return "Hello, " + name + "!"
|
||||
},
|
||||
})
|
||||
|
||||
app := iris.New()
|
||||
app.RegisterView(e)
|
||||
app.Get("/", index)
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func index(ctx iris.Context) {
|
||||
ctx.View("program.html", iris.Map{
|
||||
"Name": "Gerasimos",
|
||||
})
|
||||
}
|
||||
32
_examples/view/parse-template/jet/main.go
Normal file
32
_examples/view/parse-template/jet/main.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/view"
|
||||
)
|
||||
|
||||
func main() {
|
||||
e := iris.Jet(nil, ".jet") // You can still use a file system though.
|
||||
e.AddFunc("greet", func(args view.JetArguments) reflect.Value {
|
||||
msg := "Hello, " + args.Get(0).String() + "!"
|
||||
return reflect.ValueOf(msg)
|
||||
})
|
||||
err := e.ParseTemplate("program.jet", `<h1>{{greet(.Name)}}</h1>`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
app := iris.New()
|
||||
app.RegisterView(e)
|
||||
app.Get("/", index)
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func index(ctx iris.Context) {
|
||||
ctx.View("program.jet", iris.Map{
|
||||
"Name": "Gerasimos",
|
||||
})
|
||||
}
|
||||
@@ -1,22 +1,28 @@
|
||||
// Package main shows how to parse a template through custom byte slice content.
|
||||
// The following works with HTML, Pug and Ace template parsers.
|
||||
// To learn how you can manually parse a template from a text for the rest
|
||||
// template parsers navigate through the example's subdirectories.
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris/v12"
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
// To not load any templates from files or embedded data,
|
||||
// pass nil or empty string on the first argument:
|
||||
// view := iris.HTML(nil, ".html")
|
||||
// e := iris.HTML(nil, ".html")
|
||||
|
||||
view := iris.HTML("./views", ".html")
|
||||
view.ParseTemplate("program.html", []byte(`<h1>{{greet .Name}}</h1>`), iris.Map{
|
||||
e := iris.HTML("./views", ".html")
|
||||
// e := iris.Pug("./views",".pug")
|
||||
// e := iris.Ace("./views",".ace")
|
||||
e.ParseTemplate("program.html", []byte(`<h1>{{greet .Name}}</h1>`), iris.Map{
|
||||
"greet": func(name string) string {
|
||||
return "Hello, " + name + "!"
|
||||
},
|
||||
})
|
||||
|
||||
app.RegisterView(view)
|
||||
app := iris.New()
|
||||
app.RegisterView(e)
|
||||
|
||||
app.Get("/", index)
|
||||
app.Get("/layout", layout)
|
||||
|
||||
|
||||
@@ -10,13 +10,13 @@ func main() {
|
||||
app.Logger().SetLevel("debug")
|
||||
|
||||
// Init the handlebars engine
|
||||
engine := iris.Handlebars("./templates", ".html").Reload(true)
|
||||
e := iris.Handlebars("./templates", ".html").Reload(true)
|
||||
// Register a helper.
|
||||
engine.AddFunc("fullName", func(person map[string]string) string {
|
||||
e.AddFunc("fullName", func(person map[string]string) string {
|
||||
return person["firstName"] + " " + person["lastName"]
|
||||
})
|
||||
|
||||
app.RegisterView(engine)
|
||||
app.RegisterView(e)
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
viewData := iris.Map{
|
||||
|
||||
Reference in New Issue
Block a user