代码拉取完成,页面将自动刷新
package main
import (
"time"
"github.com/kataras/iris"
)
const (
DefaultTitle = "My Awesome Site"
DefaultLayout = "layouts/layout.html"
)
func main() {
app := iris.New()
// output startup banner and error logs on os.Stdout
// set the view engine target to ./templates folder
app.RegisterView(iris.HTML("./templates", ".html").Reload(true))
app.Use(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(ctx.Application().ConfigurationReadOnly().GetTimeFormat())
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.View("index.html"); err != nil {
ctx.Application().Logger().Infof(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.View("index.html"); err != nil {
ctx.Application().Logger().Infof(err.Error())
}
})
// http://localhost:8080
// http://localhost:8080/about
app.Run(iris.Addr(":8080"))
}
// Notes: ViewData("", myCustomStruct{}) will set this myCustomStruct value as a root binding data,
// so any View("other", "otherValue") will probably fail.
// To clear the binding data: ctx.Set(ctx.Application().ConfigurationReadOnly().GetViewDataContextKey(), nil)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。