add support for Iris framework (and Iris Multiple)

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-03 18:19:51 +03:00
parent ddf4ecda9c
commit 5cfdc88878
22 changed files with 544 additions and 5 deletions

View File

@@ -1,7 +1,5 @@
language: go
go:
- 1.11.x
- 1.12.x
- 1.13.x
- 1.14.x
- master

149
README.md
View File

@@ -11,6 +11,7 @@ Goview is a lightweight, minimalist and idiomatic template library based on gola
- [Docs](#docs)
- [Supports](#supports)
- [Gin Framework](https://github.com/foolin/goview/tree/master/supports/ginview)
- [Iris Framework](https://github.com/foolin/goview/tree/master/supports/irisview)
- [Echo Framework](https://github.com/foolin/goview/tree/master/supports/echoview)
- [Go.Rice](https://github.com/foolin/goview/tree/master/supports/gorice)
- [Usage](#usage)
@@ -21,6 +22,8 @@ Goview is a lightweight, minimalist and idiomatic template library based on gola
- [Examples](#examples)
- [Basic example](#basic-example)
- [Gin example](#gin-example)
- [Iris example](#iris-example)
- [Iris multiple example](#iris-multiple-example)
- [Echo example](#echo-example)
- [Go-chi example](#go-chi-example)
- [Advance example](#advance-example)
@@ -48,7 +51,7 @@ go get github.com/foolin/goview
* **Multiple Engine** - Support multiple templates for frontend and backend.
* **No external dependencies** - plain ol' Go html/template.
* **Gorice** - Support gorice for package resources.
* **Gin/Echo/Chi** - Support gin framework,echo framework, go-chi framework.
* **Gin/Iris/Echo/Chi** - Support gin framework, Iris framework, echo framework, go-chi framework.
## Docs
@@ -57,6 +60,7 @@ See <https://www.godoc.org/github.com/foolin/goview>
## Supports
- **[ginview](https://github.com/foolin/goview/tree/master/supports/ginview)** goview for gin framework
- **[irisview](https://github.com/foolin/goview/tree/master/supports/irisview)** goview for Iris framework
- **[echoview](https://github.com/foolin/goview/tree/master/supports/echoview)** goview for echo framework
- **[gorice](https://github.com/foolin/goview/tree/master/supports/gorice)** goview for go.rice
@@ -335,7 +339,150 @@ See in "examples/basic" folder
[Gin example](https://github.com/foolin/goview/tree/master/_examples/gin)
### Iris example
```bash
$ go get github.com/foolin/goview/supports/irisview
```
```go
package main
import (
"github.com/foolin/goview/supports/irisview"
"github.com/kataras/iris/v12"
)
func main() {
app := iris.New()
// Register the goview template engine.
app.RegisterView(irisview.Default())
app.Get("/", func(ctx iris.Context) {
// Render with master.
ctx.View("index", iris.Map{
"title": "Index title!",
"add": func(a int, b int) int {
return a + b
},
})
})
app.Get("/page", func(ctx iris.Context) {
// Render only file, must full name with extension.
ctx.View("page.html", iris.Map{"title": "Page file title!!"})
})
app.Listen(":9090")
}
```
Project structure:
```go
|-- app/views/
|--- index.html
|--- page.html
|-- layouts/
|--- footer.html
|--- master.html
See in "examples/iris" folder
```
[Iris example](https://github.com/foolin/goview/tree/master/_examples/iris)
### Iris multiple example
```go
package main
import (
"html/template"
"time"
"github.com/foolin/goview"
"github.com/foolin/goview/supports/irisview"
"github.com/kataras/iris/v12"
)
func main() {
app := iris.New()
// Register a new template engine.
app.RegisterView(irisview.New(goview.Config{
Root: "views/frontend",
Extension: ".html",
Master: "layouts/master",
Partials: []string{"partials/ad"},
Funcs: template.FuncMap{
"copy": func() string {
return time.Now().Format("2006")
},
},
DisableCache: true,
}))
app.Get("/", func(ctx iris.Context) {
ctx.View("index", iris.Map{
"title": "Frontend title!",
})
})
//=========== Backend ===========//
// Assign a new template middleware.
mw := irisview.NewMiddleware(goview.Config{
Root: "views/backend",
Extension: ".html",
Master: "layouts/master",
Partials: []string{},
Funcs: template.FuncMap{
"copy": func() string {
return time.Now().Format("2006")
},
},
DisableCache: true,
})
backendGroup := app.Party("/admin", mw)
backendGroup.Get("/", func(ctx iris.Context) {
// Use the ctx.View as you used to. Zero changes to your codebase,
// even if you use multiple templates.
ctx.View("index", iris.Map{
"title": "Backend title!",
})
})
app.Listen(":9090")
}
```
Project structure:
```go
|-- app/views/
|-- fontend/
|--- index.html
|-- layouts/
|--- footer.html
|--- head.html
|--- master.html
|-- partials/
|--- ad.html
|-- backend/
|--- index.html
|-- layouts/
|--- footer.html
|--- head.html
|--- master.html
See in "examples/iris-multiple" folder
```
[Iris multiple example](https://github.com/foolin/goview/tree/master/_examples/iris-multiple)
### Echo example

View File

@@ -0,0 +1,17 @@
# Multiple
Exmaple for Iris template, font-end and back-end.
# Run
```sh
$ go run main.go
```
# View
Use the browser to visit the following URL:
```
http://127.0.0.1:9090
```

View File

@@ -0,0 +1,70 @@
/*
* Copyright 2018 Foolin. All rights reserved.
*
* Use of this source code is governed by a MIT style
* license that can be found in the LICENSE file.
*
*/
package main
import (
"html/template"
"time"
"github.com/foolin/goview"
"github.com/foolin/goview/supports/irisview"
"github.com/kataras/iris/v12"
)
func main() {
app := iris.New()
// Register a new template engine.
app.RegisterView(irisview.New(goview.Config{
Root: "views/frontend",
Extension: ".html",
Master: "layouts/master",
Partials: []string{"partials/ad"},
Funcs: template.FuncMap{
"copy": func() string {
return time.Now().Format("2006")
},
},
DisableCache: true,
}))
app.Get("/", func(ctx iris.Context) {
ctx.View("index", iris.Map{
"title": "Frontend title!",
})
})
//=========== Backend ===========//
// Assign a new template middleware.
mw := irisview.NewMiddleware(goview.Config{
Root: "views/backend",
Extension: ".html",
Master: "layouts/master",
Partials: []string{},
Funcs: template.FuncMap{
"copy": func() string {
return time.Now().Format("2006")
},
},
DisableCache: true,
})
backendGroup := app.Party("/admin", mw)
backendGroup.Get("/", func(ctx iris.Context) {
// Use the ctx.View as you used to. Zero changes to your codebase,
// even if you use multiple templates.
ctx.View("index", iris.Map{
"title": "Backend title!",
})
})
app.Listen(":9090")
}

View File

@@ -0,0 +1,5 @@
{{define "content"}}
<h1 class="hello">This is backend!!!!</h1>
<hr>
<p><a href="/">Go to frontend</a></p>
{{end}}

View File

@@ -0,0 +1 @@
Copyright &copy {{copy}} <a href="https://github.com/foolin" target="_blank">Foolin</a>

View File

@@ -0,0 +1,4 @@
<style>
.hello{ color: red;}
hr{ border: 1px #ccc dashed;}
</style>

View File

@@ -0,0 +1,15 @@
<!-- /views/admin/master.html -->
<!doctype html>
<html>
<head>
<title>{{.title}}</title>
{{include "layouts/head"}}
</head>
<body>
{{template "content" .}}
<hr>
{{include "layouts/footer"}}
</body>
</html>

View File

@@ -0,0 +1,5 @@
{{define "content"}}
<h1 class="hello">This is frontend!!!!</h1>
<hr>
<p><a href="/admin/">Go to backend</a></p>
{{end}}

View File

@@ -0,0 +1 @@
Copyright &copy {{copy}} <a href="https://github.com/foolin" target="_blank">Foolin</a>

View File

@@ -0,0 +1,4 @@
<style>
.hello{ color: red;}
hr{ border: 1px #ccc dashed;}
</style>

View File

@@ -0,0 +1,17 @@
<!-- /views/admin/master.html -->
<!doctype html>
<html>
<head>
<title>{{.title}}</title>
{{include "layouts/head"}}
</head>
<body>
{{template "content" .}}
<hr>
{{template "ad" .}}
<hr>
{{include "layouts/footer"}}
</body>
</html>

View File

@@ -0,0 +1,3 @@
{{define "ad"}}
<p>[AD: <a href="https://github.com/foolin" target="_blank">Wellcome to fontend!</a>]</p>
{{end}}

17
_examples/iris/README.md Normal file
View File

@@ -0,0 +1,17 @@
# Basic
Exmaple for Iris template.
# Run
```sh
$ go run main.go
```
# View
Use the browser to visit the following URL:
```
http://127.0.0.1:9090
```

38
_examples/iris/main.go Normal file
View File

@@ -0,0 +1,38 @@
/*
* Copyright 2018 Foolin. All rights reserved.
*
* Use of this source code is governed by a MIT style
* license that can be found in the LICENSE file.
*
*/
package main
import (
"github.com/foolin/goview/supports/irisview"
"github.com/kataras/iris/v12"
)
func main() {
app := iris.New()
// Register the goview template engine.
app.RegisterView(irisview.Default())
app.Get("/", func(ctx iris.Context) {
// Render with master.
ctx.View("index", iris.Map{
"title": "Index title!",
"add": func(a int, b int) int {
return a + b
},
})
})
app.Get("/page", func(ctx iris.Context) {
// Render only file, must full name with extension.
ctx.View("page.html", iris.Map{"title": "Page file title!!"})
})
app.Listen(":9090")
}

View File

@@ -0,0 +1,14 @@
{{define "head"}}
<style>
.hello{ color: red;}
hr{ border: 1px #ccc dashed;}
</style>
{{end}}
{{define "content"}}
<h1 class="hello">This is content!!!!</h1>
<p>123 + 333 = {{call $.add 123 333}}</p>
<hr>
<p><a href="/page">Page render</a></p>
{{end}}

View File

@@ -0,0 +1 @@
Copyright &copy2018 <a href="https://github.com/foolin" target="_blank">Foolin</a>

View File

@@ -0,0 +1,15 @@
<!-- /views/admin/master.html -->
<!doctype html>
<html>
<head>
<title>{{.title}}</title>
{{template "head" .}}
</head>
<body>
{{template "content" .}}
<hr>
{{include "layouts/footer"}}
</body>
</html>

View File

@@ -0,0 +1,19 @@
<!-- /views/page.html -->
<!doctype html>
<html>
<head>
<title>{{.title}}</title>
</head>
<body>
<a href="/"><- Back home!</a>
<hr>
This page not use master, Render code:
<pre>ctx.View("page.html", iris.Map{"title": "Page file title!!"})</pre>
<br>
"page.html" - add extension ".html" will render without master.
<hr>
{{include "layouts/footer"}}
</body>
</html>

3
go.mod
View File

@@ -1,10 +1,11 @@
module github.com/foolin/goview
go 1.12
go 1.14
require (
github.com/GeertJohan/go.rice v1.0.0
github.com/gin-gonic/gin v1.4.0
github.com/kataras/iris/v12 v12.1.8
github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/echo/v4 v4.1.6
)

View File

@@ -0,0 +1,69 @@
# IrisView
[![GoDoc Widget]][GoDoc]
goview support for Iris template.
## Install
```sh
$ go get -u github.com/foolin/goview
$ go get -u github.com/foolin/goview/supports/irisview
```
### Example
```go
package main
import (
"github.com/foolin/goview/supports/irisview"
"github.com/kataras/iris/v12"
)
func main() {
app := iris.New()
// Register the goview template engine.
app.RegisterView(irisview.Default())
app.Get("/", func(ctx iris.Context) {
// Render with master.
ctx.View("index", iris.Map{
"title": "Index title!",
"add": func(a int, b int) int {
return a + b
},
})
})
app.Get("/page", func(ctx iris.Context) {
// Render only file, must full name with extension.
ctx.View("page.html", iris.Map{"title": "Page file title!!"})
})
app.Listen(":9090")
}
```
Project structure:
```go
|-- app/views/
|--- index.html
|--- page.html
|-- layouts/
|--- footer.html
|--- master.html
See in "examples/basic" folder
```
[Iris example](https://github.com/foolin/goview/tree/master/_examples/iris)
## More examples
See [_examples/](https://github.com/foolin/goview/blob/master/_examples/) for a variety of examples.
[GoDoc]: https://godoc.org/github.com/foolin/goview/supports/irisview
[GoDoc Widget]: https://godoc.org/github.com/foolin/goview/supports/irisview?status.svg

View File

@@ -0,0 +1,78 @@
package irisview
import (
"io"
"github.com/foolin/goview"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/view"
)
const templateEngineKey = "iris.foolin.goview"
// ViewEngine view engine for Iris.
type ViewEngine struct {
*goview.ViewEngine
ext string
}
var _ view.Engine = (*ViewEngine)(nil)
// New new view engine for Iris.
func New(config goview.Config) *ViewEngine {
e := Wrap(goview.New(config))
e.ext = config.Extension
return e
}
// Wrap wraps a view engine for goview.ViewEngine.
func Wrap(engine *goview.ViewEngine) *ViewEngine {
return &ViewEngine{
ViewEngine: engine,
ext: goview.DefaultConfig.Extension,
}
}
// Default returns a new default view engine.
func Default() *ViewEngine {
return New(goview.DefaultConfig)
}
// Load does nothing here, templates are loaded through goview.
func (e *ViewEngine) Load() error {
return nil
}
// Ext returns the file extension, it's empty on this case because
// the goviw engine supports filenames without extension.
func (e *ViewEngine) Ext() string {
return ""
}
// ExecuteWriter executes a template by its name.
// It supports multiple templates, see `NewMiddleware` and `Middleware` too.
func (e *ViewEngine) ExecuteWriter(w io.Writer, filename string, _ string, bindingData interface{}) error {
if ctx, ok := w.(iris.Context); ok {
if v := ctx.Values().Get(templateEngineKey); v != nil {
if e, ok := v.(*ViewEngine); ok {
return e.ViewEngine.RenderWriter(w, filename, bindingData)
}
}
}
return e.ViewEngine.RenderWriter(w, filename, bindingData)
}
// NewMiddleware Iris middleware for multiple templates.
func NewMiddleware(config goview.Config) iris.Handler {
return Middleware(New(config))
}
// Middleware Iris middleware wrapper.
func Middleware(e *ViewEngine) iris.Handler {
return func(ctx iris.Context) {
ctx.Values().Set(templateEngineKey, e)
ctx.Next()
}
}