1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-10 05:25:58 +00:00

builtin html template functions changes

This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-12-13 01:37:15 +02:00
parent abeae40e60
commit 1ea5cd58be
115 changed files with 472 additions and 230 deletions

View File

@@ -5,6 +5,6 @@
</head>
<body>
<!-- Render the current template here -->
{{ yield }}
{{ yield . . }}
</body>
</html>

View File

@@ -37,7 +37,10 @@ func index(ctx iris.Context) {
}
ctx.ViewLayout("main")
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
func admin(ctx iris.Context) {
@@ -46,7 +49,10 @@ func admin(ctx iris.Context) {
}
ctx.ViewLayout("main")
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
func setViews(views iris.ViewEngine) iris.Handler {
@@ -61,5 +67,8 @@ func onFly(ctx iris.Context) {
"Message": "View engine changed through 'setViews' custom middleware.",
}
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -8,6 +8,6 @@
<body>
{{ template "content" . }}
<footer>{{ partial "partials/footer" .}}</footer>
<footer>{{ partial "partials/footer" . }}</footer>
</body>
</html>

View File

@@ -37,10 +37,16 @@ func main() {
my := app.Party("/my").Layout("layouts/mylayout.html")
{ // both of these will use the layouts/mylayout.html as their layout.
my.Get("/", func(ctx iris.Context) {
ctx.View("page1.html")
if err := ctx.View("page1.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
my.Get("/other", func(ctx iris.Context) {
ctx.View("page1.html")
if err := ctx.View("page1.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
}

View File

@@ -7,6 +7,6 @@
<h1>This is the global layout</h1>
<br />
<!-- Render the current template here -->
{{ yield }}
{{ yield . }}
</body>
</html>

View File

@@ -7,6 +7,6 @@
<h1>This is the layout for the /my/ and /my/other routes only</h1>
<br />
<!-- Render the current template here -->
{{ yield }}
{{ yield . }}
</body>
</html>

View File

@@ -2,6 +2,6 @@
<h1>Page 1 {{ greet "iris developer"}}</h1>
{{ render "partials/page1_partial1.html"}}
{{ render "partials/page1_partial1.html" . }}
</div>

View File

@@ -7,6 +7,6 @@
<h1>This is the global layout</h1>
<br />
<!-- Render the current template here -->
{{ yield }}
{{ yield . }}
</body>
</html>

View File

@@ -7,6 +7,6 @@
<h1>This is the layout for the /my/ and /my/other routes only</h1>
<br />
<!-- Render the current template here -->
{{ yield }}
{{ yield . }}
</body>
</html>

View File

@@ -2,6 +2,6 @@
<h1>Page 1 {{ greet "iris developer"}}</h1>
{{ render "partials/page1_partial1.html"}}
{{ render "partials/page1_partial1.html" . }}
</div>

View File

@@ -4,7 +4,6 @@ import (
"embed"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/x/errors"
)
//go:embed embedded/*
@@ -24,7 +23,7 @@ func main() {
app.Get("/", func(ctx iris.Context) {
if err := ctx.View("page1.html"); err != nil {
errors.InvalidArgument.Err(ctx, err)
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
@@ -33,7 +32,7 @@ func main() {
app.Get("/nolayout", func(ctx iris.Context) {
ctx.ViewLayout(iris.NoLayout)
if err := ctx.View("page1.html"); err != nil {
errors.InvalidArgument.Err(ctx, err)
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
@@ -42,10 +41,16 @@ func main() {
my := app.Party("/my").Layout("layouts/mylayout.html")
{ // both of these will use the layouts/mylayout.html as their layout.
my.Get("/", func(ctx iris.Context) {
ctx.View("page1.html")
if err := ctx.View("page1.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
my.Get("/other", func(ctx iris.Context) {
ctx.View("page1.html")
if err := ctx.View("page1.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
}

View File

@@ -38,5 +38,8 @@ func main() {
// }
func index(ctx iris.Context) {
ctx.View("blabla.html")
if err := ctx.View("blabla.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -22,5 +22,8 @@ func index(ctx iris.Context) {
}
ctx.ViewLayout("layouts/main")
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -3,6 +3,6 @@ html
head
title {{.Title}}
body
{{ yield }}
{{ yield . }}
footer
= include partials/footer.ace .

View File

@@ -21,5 +21,8 @@ func index(ctx iris.Context) {
// On Amber this is ignored: ctx.ViewLayout("layouts/main")
// Layouts are only rendered from inside the index page itself
// using the "extends" keyword.
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -22,5 +22,8 @@ func index(ctx iris.Context) {
}
ctx.ViewLayout("main")
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -5,7 +5,7 @@
<body>
{{ template "content" . }}
<footer>
{{ partial "partials/footer" .}}
{{ partial "partials/footer" . }}
</footer>
</body>
</html>

View File

@@ -21,5 +21,8 @@ func index(ctx iris.Context) {
// On Django this is ignored: ctx.ViewLayout("layouts/main")
// Layouts are only rendered from inside the index page itself
// using the "extends" keyword.
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -20,5 +20,8 @@ func index(ctx iris.Context) {
}
ctx.ViewLayout("layouts/main")
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -3,7 +3,7 @@
<title>{{Title}}</title>
</head>
<body>
{{ yield }}
{{ yield . }}
<footer>{{ render "partials/footer.html" .}}</footer>
</body>

View File

@@ -20,5 +20,8 @@ func index(ctx iris.Context) {
}
ctx.ViewLayout("layouts/main")
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -3,9 +3,9 @@
<title>{{.Title}}</title>
</head>
<body>
{{ yield }}
{{ yield . }}
<footer>
{{ render "partials/footer.html" }}
{{ render "partials/footer.html" . }}
</footer>
</body>
</html>

View File

@@ -22,5 +22,8 @@ func index(ctx iris.Context) {
// On Jet this is ignored: ctx.ViewLayout("layouts/main")
// Layouts are only rendered from inside the index page itself
// using the "extends" keyword.
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -3,7 +3,7 @@
<title>{{.Title}}</title>
</head>
<body>
{{ yield documentBody() }}
{{ yield . documentBody() }}
<footer>{{ include "../partials/footer.jet" . }}</footer>
</body>
</html>

View File

@@ -21,5 +21,8 @@ func index(ctx iris.Context) {
// On Pug this is ignored: ctx.ViewLayout("layouts/main")
// Layouts are only rendered from inside the index page itself
// using the "extends" keyword.
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -8,10 +8,10 @@ func main() {
// with default template funcs:
//
// - {{ urlpath "mynamedroute" "pathParameter_ifneeded" }}
// - {{ render "header.html" }}
// - {{ render_r "header.html" }} // partial relative path to current page
// - {{ yield }}
// - {{ current }}
// - {{ render "header.html" . }}
// - {{ render_r "header.html" . }} // partial relative path to current page
// - {{ yield . }}
// - {{ current . }}
app.RegisterView(iris.HTML("./templates", ".html").
Reload(true)) // Set Reload false to production.
@@ -23,17 +23,23 @@ func main() {
ctx.ViewData("Name", "iris")
// render the template with the file name relative to the './templates'.
// file extension is OPTIONAL.
ctx.View("hi.html")
if err := ctx.View("hi.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
app.Get("/example_map", func(ctx iris.Context) {
ctx.View("example.html", iris.Map{
if err := ctx.View("example.html", iris.Map{
"Name": "Example Name",
"Age": 42,
"Items": []string{"Example slice entry 1", "entry 2", "entry 3"},
"Map": iris.Map{"map key": "map value", "other key": "other value"},
"Nested": iris.Map{"Title": "Iris E-Book", "Pages": 620},
})
}); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
app.Get("/example_struct", func(ctx iris.Context) {
@@ -59,7 +65,10 @@ func main() {
},
}
ctx.View("example.html", examplePage)
if err := ctx.View("example.html", examplePage); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
// http://localhost:8080/

View File

@@ -21,9 +21,12 @@ func main() {
}
func index(ctx iris.Context) {
ctx.View("program.amber", iris.Map{
if err := ctx.View("program.amber", iris.Map{
"Name": "Gerasimos",
// Or per template:
// "greet": func(....)
})
}); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -21,7 +21,10 @@ func main() {
}
func index(ctx iris.Context) {
ctx.View("program.html", iris.Map{
if err := ctx.View("program.html", iris.Map{
"Name": "Gerasimos",
})
}); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -19,7 +19,10 @@ func main() {
}
func index(ctx iris.Context) {
ctx.View("program.html", iris.Map{
if err := ctx.View("program.html", iris.Map{
"Name": "Gerasimos",
})
}); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -27,7 +27,10 @@ func main() {
}
func index(ctx iris.Context) {
ctx.View("program.jet", iris.Map{
if err := ctx.View("program.jet", iris.Map{
"Name": "Gerasimos",
})
}); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -31,9 +31,12 @@ func main() {
}
func index(ctx iris.Context) {
ctx.View("program.html", iris.Map{
if err := ctx.View("program.html", iris.Map{
"Name": "Gerasimos",
})
}); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
func layout(ctx iris.Context) {

View File

@@ -6,6 +6,6 @@
<body>
<h1>[layout] Body content is below...</h1>
<!-- Render the current template here -->
{{ yield }}
{{ yield . }}
</body>
</html>

View File

@@ -12,16 +12,22 @@ func main() {
app.RegisterView(tmpl)
app.Get("/", func(ctx iris.Context) {
ctx.View("index", iris.Map{
if err := ctx.View("index", iris.Map{
"Title": "Title of The Page",
})
}); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
app.Get("/layout", func(ctx iris.Context) {
ctx.ViewLayout("layouts/main") // layout for that response.
ctx.View("index", iris.Map{ // file extension is optional.
ctx.ViewLayout("layouts/main") // layout for that response.
if err := ctx.View("index", iris.Map{ // file extension is optional.
"Title": "Title of the main Page",
})
}); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
// otherGroup := app.Party("/other").Layout("layouts/other.ace") -> layout for that party.

View File

@@ -4,4 +4,4 @@ html
title Main Page
body
h1 Layout
{{ yield }}
{{ yield . }}

View File

@@ -11,9 +11,12 @@ func main() {
app.RegisterView(tmpl)
app.Get("/", func(ctx iris.Context) {
ctx.View("index.amber", iris.Map{
if err := ctx.View("index.amber", iris.Map{
"Title": "Title of The Page",
})
}); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
app.Listen(":8080")

View File

@@ -19,9 +19,12 @@ func main() {
app.RegisterView(tmpl)
app.Get("/", func(ctx iris.Context) {
ctx.View("index.amber", iris.Map{
if err := ctx.View("index.amber", iris.Map{
"Title": "Title of The Page",
})
}); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
app.Listen(":8080")

View File

@@ -19,7 +19,10 @@ func index(ctx iris.Context) {
}
ctx.ViewLayout("main")
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
func internalServerError(ctx iris.Context) {
@@ -31,5 +34,8 @@ func internalServerError(ctx iris.Context) {
}
ctx.ViewLayout("error")
ctx.View("500", data)
if err := ctx.View("500", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -8,6 +8,6 @@
<body>
{{ template "content" . }}
<footer>{{ partial "partials/footer" .}}</footer>
<footer>{{ partial "partials/footer" . }}</footer>
</body>
</html>

View File

@@ -28,7 +28,10 @@ func index(ctx iris.Context) {
}
ctx.ViewLayout("main")
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
func internalServerError(ctx iris.Context) {
@@ -40,5 +43,8 @@ func internalServerError(ctx iris.Context) {
}
ctx.ViewLayout("error")
ctx.View("500", data)
if err := ctx.View("500", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -35,9 +35,12 @@ func hi(ctx iris.Context) {
// or if you set all view data in the same handler you can use the
// iris.Map/pongo2.Context/map[string]interface{}, look below:
ctx.View("hi.html", iris.Map{
if err := ctx.View("hi.html", iris.Map{
"title": "Hi Page",
"name": "iris",
"serverStartTime": startTime,
})
}); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -31,7 +31,8 @@ func main() {
paramsAsArray := []string{"theParam1", "theParam2", "paramThirdAfterStatic"}
ctx.ViewData("ParamsAsArray", paramsAsArray)
if err := ctx.View("page.html"); err != nil {
panic(err)
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})

View File

@@ -28,7 +28,10 @@ func main() {
}},
}
ctx.View("example.html", viewData)
if err := ctx.View("example.html", viewData); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
exampleRouter := app.Party("/example")

View File

@@ -12,10 +12,10 @@ func main() {
// default template funcs are:
//
// - {{ urlpath "mynamedroute" "pathParameter_ifneeded" }}
// - {{ render "header.html" }}
// - {{ render_r "header.html" }} // partial relative path to current page
// - {{ yield }}
// - {{ current }}
// - {{ render "header.html" . }}
// - {{ render_r "header.html" . }} // partial relative path to current page
// - {{ yield . }}
// - {{ current . }}
tmpl.AddFunc("greet", func(s string) string {
return "Greetings " + s + "!"
})
@@ -31,5 +31,8 @@ func hi(ctx iris.Context) {
ctx.ViewData("Title", "Hi Page")
ctx.ViewData("Name", "iris") // {{.Name}} will render: iris
// ctx.ViewData("", myCcustomStruct{})
ctx.View("hi.html")
if err := ctx.View("hi.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -18,7 +18,10 @@ func main() {
app.Get("/", func(ctx iris.Context) {
ctx.CompressWriter(true)
ctx.ViewData("", mypage{"My Page title", "Hello world!"})
ctx.View("mypage.html")
if err := ctx.View("mypage.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
// Note that: you can pass "layout" : "otherLayout.html" to bypass the config's Layout property
// or view.NoLayout to disable layout on this render action.
// third is an optional parameter

View File

@@ -6,6 +6,6 @@
<body>
<h1>[layout] Body content is below...</h1>
<!-- Render the current template here -->
{{ yield }}
{{ yield . }}
</body>
</html>

View File

@@ -35,10 +35,16 @@ func main() {
my := app.Party("/my").Layout("layouts/mylayout.html")
{ // both of these will use the layouts/mylayout.html as their layout.
my.Get("/", func(ctx iris.Context) {
ctx.View("page1.html")
if err := ctx.View("page1.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
my.Get("/other", func(ctx iris.Context) {
ctx.View("page1.html")
if err := ctx.View("page1.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
}

View File

@@ -7,6 +7,6 @@
<h1>This is the global layout</h1>
<br />
<!-- Render the current template here -->
{{ yield }}
{{ yield . }}
</body>
</html>

View File

@@ -7,6 +7,6 @@
<h1>This is the layout for the /my/ and /my/other routes only</h1>
<br />
<!-- Render the current template here -->
{{ yield }}
{{ yield . }}
</body>
</html>

View File

@@ -2,6 +2,6 @@
<h1>Page 1 {{ greet "iris developer"}}</h1>
{{ render "partials/page1_partial1.html" }}
{{ render "partials/page1_partial1.html" . }}
</div>

View File

@@ -35,7 +35,8 @@ func main() {
paramsAsArray := []string{"theParam1", "theParam2", "paramThirdAfterStatic"}
ctx.ViewData("ParamsAsArray", paramsAsArray)
if err := ctx.View("page.html"); err != nil {
panic(err)
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})

View File

@@ -52,7 +52,8 @@ func main() {
paramsAsArray := []string{"username5", "theParam1", "theParam2", "paramThirdAfterStatic"}
ctx.ViewData("ParamsAsArray", paramsAsArray)
if err := ctx.View("page.html"); err != nil {
panic(err)
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})

View File

@@ -12,18 +12,28 @@ func main() {
app.Get("/home", func(ctx iris.Context) {
ctx.ViewData("title", "Home page")
ctx.View("home.html")
if err := ctx.View("home.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
// Note that: you can pass "layout" : "otherLayout.html" to bypass the config's Layout property
// or view.NoLayout to disable layout on this render action.
// third is an optional parameter
})
app.Get("/about", func(ctx iris.Context) {
ctx.View("about.html")
if err := ctx.View("about.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
app.Get("/user/index", func(ctx iris.Context) {
ctx.View("user/index.html")
if err := ctx.View("user/index.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
// http://localhost:8080

View File

@@ -1,11 +1,11 @@
<html>
<head>
{{ part "head" }}
{{ part "head"}}
</head>
<body>
<h1>[layout] Body content is below...</h1>
{{ part "body" }}
{{ part "body" . }}
<!-- Render the current template here -->
{{ yield }}
{{ yield . }}
</body>
</html>

View File

@@ -113,7 +113,10 @@ func main() {
}
ctx.ViewData("title", "Show TODO")
ctx.View("todos/show.jet", todo)
if err := ctx.View("todos/show.jet", todo); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
app.Get("/all-done", func(ctx iris.Context) {
// vars := make(view.JetRuntimeVars)
@@ -132,7 +135,10 @@ func main() {
// ctx.ViewData("_jet", (&doneTODOs{}).New(todos))
// and ctx.View("todos/index.jet")
// OR
ctx.View("todos/index.jet", (&doneTODOs{}).New(todos))
if err := ctx.View("todos/index.jet", (&doneTODOs{}).New(todos)); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
port := os.Getenv("PORT")

View File

@@ -22,7 +22,10 @@ func main() {
app.RegisterView(tmpl)
app.Get("/", func(ctx iris.Context) {
ctx.View("index.jet")
if err := ctx.View("index.jet"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
addr := ":8080"

View File

@@ -34,7 +34,8 @@ func main() {
paramsAsArray := []string{"theParam1", "theParam2", "paramThirdAfterStatic"}
ctx.ViewData("ParamsAsArray", paramsAsArray)
if err := ctx.View("page.jet"); err != nil {
panic(err)
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})

View File

@@ -24,7 +24,10 @@ func main() {
app.RegisterView(tmpl)
app.Get("/", func(ctx iris.Context) {
ctx.View("index.jet")
if err := ctx.View("index.jet"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
app.Listen(":8080")

View File

@@ -38,5 +38,8 @@ func index(ctx iris.Context) {
Jobs: []*Job{&job1, &job2},
}
ctx.View("index.pug", person)
if err := ctx.View("index.pug", person); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -24,5 +24,8 @@ func main() {
}
func index(ctx iris.Context) {
ctx.View("index.pug")
if err := ctx.View("index.pug"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

View File

@@ -22,5 +22,8 @@ func main() {
}
func index(ctx iris.Context) {
ctx.View("index.pug")
if err := ctx.View("index.pug"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}