mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 20:41:57 +00:00
add a ParseTemplate to the HTML view engine.
relative to: #1617 Wait for an answer from the issuer and if that's the expected behavior, do the same for the rest of the view engines
This commit is contained in:
@@ -110,6 +110,7 @@
|
||||
* [Inject Engine Between Handlers](view/context-view-engine/main.go)
|
||||
* [Embedding Templates Into App Executable File](view/embedding-templates-into-app/main.go)
|
||||
* [Write to a custom `io.Writer`](view/write-to)
|
||||
* [Parse a Template manually](view/parse-template/main.go)
|
||||
* [Blocks](view/template_blocks_0)
|
||||
* [Blocks Embedded](view/template_blocks_1_embedded)
|
||||
* [Pug: `Actions`](view/template_pug_0)
|
||||
|
||||
35
_examples/view/parse-template/main.go
Normal file
35
_examples/view/parse-template/main.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// Package main shows how to parse a template through custom byte slice content.
|
||||
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")
|
||||
|
||||
view := iris.HTML("./views", ".html")
|
||||
view.ParseTemplate("program.html", []byte(`<h1>{{greet .Name}}</h1>`), iris.Map{
|
||||
"greet": func(name string) string {
|
||||
return "Hello, " + name + "!"
|
||||
},
|
||||
})
|
||||
|
||||
app.RegisterView(view)
|
||||
app.Get("/", index)
|
||||
app.Get("/layout", layout)
|
||||
|
||||
app.Listen(":8080")
|
||||
}
|
||||
|
||||
func index(ctx iris.Context) {
|
||||
ctx.View("program.html", iris.Map{
|
||||
"Name": "Gerasimos",
|
||||
})
|
||||
}
|
||||
|
||||
func layout(ctx iris.Context) {
|
||||
ctx.ViewLayout("layouts/main.html")
|
||||
index(ctx)
|
||||
}
|
||||
11
_examples/view/parse-template/views/layouts/main.html
Normal file
11
_examples/view/parse-template/views/layouts/main.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>My Layout</title>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<h1>[layout] Body content is below...</h1>
|
||||
<!-- Render the current template here -->
|
||||
{{ yield }}
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user