mirror of
https://github.com/kataras/iris.git
synced 2026-01-18 17:35:57 +00:00
context.ViewData(key,value) and ViewLayout(layout) http://support.iris-go.com/d/27-using-middleware-to-inject-properties-for-templates/3
Nothing that you couldn't do before. Former-commit-id: 658ec25d8045d25a76f87c8f992e67e64006e287
This commit is contained in:
@@ -38,12 +38,13 @@ It doesn't contains "best ways" neither explains all its features. It's just a s
|
||||
* [Custom HTTP Server](intermediate/custom-httpserver/main.go)
|
||||
* [View Engine](intermediate/view)
|
||||
* [Overview](intermediate/view/overview/main.go)
|
||||
* [Embedding Templates Into Executable](intermediate/embedding-templates-into-app)
|
||||
* [Template HTML: Part Zero](intermediate/view/template_html_0/main.go)
|
||||
* [Template HTML: Part One](intermediate/view/template_html_1/main.go)
|
||||
* [Template HTML: Part Two](intermediate/view/template_html_2/main.go)
|
||||
* [Template HTML: Part Three](intermediate/view/template_html_3/main.go)
|
||||
* [Template HTML: Part Four](intermediate/view/template_html_4/main.go)
|
||||
* [Inject Data Between Handlers](intermediate/view/context-view-data/main.go)
|
||||
* [Embedding Templates Into Executable](intermediate/embedding-templates-into-app)
|
||||
* [Custom Renderer](intermediate/view/custom-renderer/main.go)
|
||||
* [Password Hashing](intermediate/password-hashing/main.go)
|
||||
* [Sessions](intermediate/sessions)
|
||||
|
||||
58
_examples/intermediate/view/context-view-data/main.go
Normal file
58
_examples/intermediate/view/context-view-data/main.go
Normal file
@@ -0,0 +1,58 @@
|
||||
// this example will show you how you can set per-request data for a template outside of the main handler which calls
|
||||
// the .Render, via middleware.
|
||||
//
|
||||
// Remember: .Render has the "binding" argument which can be used to send data to the template at any case.
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gopkg.in/kataras/iris.v6"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/httprouter"
|
||||
"gopkg.in/kataras/iris.v6/adaptors/view"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultTitle = "My Awesome Site"
|
||||
DefaultLayout = "layouts/layout.html"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
// output startup banner and error logs on os.Stdout
|
||||
app.Adapt(iris.DevLogger())
|
||||
// set the router, you can choose gorillamux too
|
||||
app.Adapt(httprouter.New())
|
||||
// set the view engine target to ./templates folder
|
||||
app.Adapt(view.HTML("./templates", ".html").Reload(true))
|
||||
|
||||
app.UseFunc(func(ctx *iris.Context) {
|
||||
// set the title, current time and a layout in order to be used if and when the next handler(s) calls the .Render function
|
||||
ctx.ViewData("Title", DefaultTitle)
|
||||
now := time.Now().Format(app.Config.TimeFormat)
|
||||
ctx.ViewData("CurrentTime", now)
|
||||
ctx.ViewLayout(DefaultLayout)
|
||||
|
||||
ctx.Next()
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx *iris.Context) {
|
||||
ctx.ViewData("BodyMessage", "a sample text here... setted by the route handler")
|
||||
if err := ctx.Render("index.html", nil); err != nil {
|
||||
app.Log(iris.DevMode, err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/about", func(ctx *iris.Context) {
|
||||
ctx.ViewData("Title", "My About Page")
|
||||
ctx.ViewData("BodyMessage", "about text here... setted by the route handler")
|
||||
|
||||
// same file, just to keep things simple.
|
||||
if err := ctx.Render("index.html", nil); err != nil {
|
||||
app.Log(iris.DevMode, err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
// Open localhost:8080 and localhost:8080/about
|
||||
app.Listen(":8080")
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<h1>
|
||||
Title: {{.Title}}
|
||||
</h1>
|
||||
<h3>{{.BodyMessage}} </h3>
|
||||
|
||||
<hr/>
|
||||
|
||||
Current time: {{.CurrentTime}}
|
||||
@@ -0,0 +1,10 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>My WebsiteLayout</title>
|
||||
|
||||
</head>
|
||||
<body>
|
||||
<!-- Render the current template here -->
|
||||
{{ yield }}
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user