mirror of
https://github.com/foolin/goview.git
synced 2025-09-27 03:06:23 +08:00
add support for Iris framework (and Iris Multiple)
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.11.x
|
||||
- 1.12.x
|
||||
- 1.13.x
|
||||
- 1.14.x
|
||||
- master
|
||||
|
149
README.md
149
README.md
@@ -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
|
||||
|
||||
|
17
_examples/iris-multiple/README.md
Normal file
17
_examples/iris-multiple/README.md
Normal 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
|
||||
```
|
70
_examples/iris-multiple/main.go
Normal file
70
_examples/iris-multiple/main.go
Normal 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")
|
||||
}
|
5
_examples/iris-multiple/views/backend/index.html
Normal file
5
_examples/iris-multiple/views/backend/index.html
Normal file
@@ -0,0 +1,5 @@
|
||||
{{define "content"}}
|
||||
<h1 class="hello">This is backend!!!!</h1>
|
||||
<hr>
|
||||
<p><a href="/">Go to frontend</a></p>
|
||||
{{end}}
|
@@ -0,0 +1 @@
|
||||
Copyright © {{copy}} <a href="https://github.com/foolin" target="_blank">Foolin</a>
|
4
_examples/iris-multiple/views/backend/layouts/head.html
Normal file
4
_examples/iris-multiple/views/backend/layouts/head.html
Normal file
@@ -0,0 +1,4 @@
|
||||
<style>
|
||||
.hello{ color: red;}
|
||||
hr{ border: 1px #ccc dashed;}
|
||||
</style>
|
15
_examples/iris-multiple/views/backend/layouts/master.html
Normal file
15
_examples/iris-multiple/views/backend/layouts/master.html
Normal 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>
|
5
_examples/iris-multiple/views/frontend/index.html
Normal file
5
_examples/iris-multiple/views/frontend/index.html
Normal 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}}
|
@@ -0,0 +1 @@
|
||||
Copyright © {{copy}} <a href="https://github.com/foolin" target="_blank">Foolin</a>
|
4
_examples/iris-multiple/views/frontend/layouts/head.html
Normal file
4
_examples/iris-multiple/views/frontend/layouts/head.html
Normal file
@@ -0,0 +1,4 @@
|
||||
<style>
|
||||
.hello{ color: red;}
|
||||
hr{ border: 1px #ccc dashed;}
|
||||
</style>
|
17
_examples/iris-multiple/views/frontend/layouts/master.html
Normal file
17
_examples/iris-multiple/views/frontend/layouts/master.html
Normal 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>
|
3
_examples/iris-multiple/views/frontend/partials/ad.html
Normal file
3
_examples/iris-multiple/views/frontend/partials/ad.html
Normal 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
17
_examples/iris/README.md
Normal 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
38
_examples/iris/main.go
Normal 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")
|
||||
}
|
14
_examples/iris/views/index.html
Normal file
14
_examples/iris/views/index.html
Normal 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}}
|
1
_examples/iris/views/layouts/footer.html
Normal file
1
_examples/iris/views/layouts/footer.html
Normal file
@@ -0,0 +1 @@
|
||||
Copyright ©2018 <a href="https://github.com/foolin" target="_blank">Foolin</a>
|
15
_examples/iris/views/layouts/master.html
Normal file
15
_examples/iris/views/layouts/master.html
Normal 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>
|
19
_examples/iris/views/page.html
Normal file
19
_examples/iris/views/page.html
Normal 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
3
go.mod
@@ -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
|
||||
)
|
||||
|
69
supports/irisview/README.md
Normal file
69
supports/irisview/README.md
Normal 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
|
78
supports/irisview/irisview.go
Normal file
78
supports/irisview/irisview.go
Normal 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()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user