Skip to content

Commit

Permalink
examples: add an example for #2019
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Dec 16, 2022
1 parent 1ea5cd5 commit de8883f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ $ go get github.com/kataras/iris/v12@master
**Run**

```sh
$ go mod tidy -compat=1.19
$ go mod tidy -compat=1.19 # -compat="1.19" for windows.
$ go run .
```

Expand Down
2 changes: 1 addition & 1 deletion README_PT_BR.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ $ go get github.com/kataras/iris/v12@master
**Run**

```sh
$ go mod tidy -compat=1.19
$ go mod tidy -compat=1.19 # -compat="1.19" for windows.
$ go run .
```

Expand Down
39 changes: 35 additions & 4 deletions _examples/view/overview/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
package main

import "github.com/kataras/iris/v12"
import (
"html/template"
"time"

"github.com/kataras/iris/v12"
)

// ViewFunctions presents some builtin functions
// for html view engines. See `View.Funcs` or `view/html.Funcs` and etc.
var Functions = template.FuncMap{
"Now": time.Now,
}

func main() {
app := iris.New()
Expand All @@ -13,7 +24,8 @@ func main() {
// - {{ yield . }}
// - {{ current . }}
app.RegisterView(iris.HTML("./templates", ".html").
Reload(true)) // Set Reload false to production.
Funcs(Functions). // Optionally register some more builtin functions.
Reload(false)) // Set Reload to true on development.

app.Get("/", func(ctx iris.Context) {
// enable compression based on Accept-Encoding (e.g. "gzip"),
Expand All @@ -30,13 +42,15 @@ func main() {
})

app.Get("/example_map", func(ctx iris.Context) {
if err := ctx.View("example.html", iris.Map{
examplePage := 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 {
}

if err := ctx.View("example.html", examplePage); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
Expand Down Expand Up @@ -71,6 +85,23 @@ func main() {
}
})

app.Get("/functions", func(ctx iris.Context) {
var functionsPage = struct {
// A function.
Now func() time.Time
// A struct field which contains methods.
Ctx iris.Context
}{
Now: time.Now,
Ctx: ctx,
}

if err := ctx.View("functions.html", functionsPage); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})

// http://localhost:8080/
app.Listen(":8080")
}
3 changes: 3 additions & 0 deletions _examples/view/overview/templates/functions.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Function: {{ Now }}</h1>
<h1>Field: {{ .Ctx.Request.URL.Path }} </h1>
<h1>Field Struct's Function (Method): {{ .Ctx.FullRequestURI }} </h1>
3 changes: 2 additions & 1 deletion iris.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ type Application struct {
minifier *minify.M

// view engine
view view.View
view *view.View
// used for build
builded bool
defaultMode bool
Expand Down Expand Up @@ -120,6 +120,7 @@ func New() *Application {
Router: router.NewRouter(),
I18n: i18n.New(),
minifier: newMinifier(),
view: new(view.View),
}

logger := newLogger(app)
Expand Down
16 changes: 16 additions & 0 deletions view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package view

import (
"fmt"
"html/template"
"io"
"strings"

Expand Down Expand Up @@ -76,6 +77,21 @@ func (v *View) AddFunc(funcName string, funcBody interface{}) {
}
}

// Funcs registers a template func map to the registered view engine(s).
func (v *View) Funcs(m template.FuncMap) *View {
if !v.Registered() {
return v
}

if e, ok := v.Engine.(EngineFuncer); ok {
for k, v := range m {
e.AddFunc(k, v)
}
}

return v
}

// Load compiles all the registered engines.
func (v *View) Load() error {
if !v.Registered() {
Expand Down

0 comments on commit de8883f

Please sign in to comment.