1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 09:57:01 +00:00

Ability to register a view engine per group of routes or for the current a chain of handlers

Example at: https://github.com/kataras/iris/tree/master/_examples/view/context-view-engine
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-05 19:34:55 +03:00
parent b363492cca
commit 5d480dc801
22 changed files with 282 additions and 66 deletions

View File

@@ -21,10 +21,11 @@ func Ace(directory, extension string) *HTMLEngine {
once := new(sync.Once)
s.middleware = func(name string, text []byte) (contents string, err error) {
once.Do(func() { // on first template parse, all funcs are given.
for k, v := range s.funcs {
for k, v := range emptyFuncs {
funcs[k] = v
}
for k, v := range emptyFuncs {
for k, v := range s.funcs {
funcs[k] = v
}
})

View File

@@ -1,31 +0,0 @@
package view
import (
"io"
)
// NoLayout disables the configuration's layout for a specific execution.
const NoLayout = "iris.nolayout"
// returns empty if it's no layout or empty layout and empty configuration's layout.
func getLayout(layout string, globalLayout string) string {
if layout == NoLayout {
return ""
}
if layout == "" && globalLayout != "" {
return globalLayout
}
return layout
}
// Engine is the interface which all view engines should be implemented in order to be registered inside iris.
type Engine interface {
// Load should load the templates from a physical system directory or by an embedded one (assets/go-bindata).
Load() error
// ExecuteWriter should execute a template by its filename with an optional layout and bindingData.
ExecuteWriter(w io.Writer, filename string, layout string, bindingData interface{}) error
// Ext should return the final file extension which this view engine is responsible to render.
Ext() string
}

View File

@@ -1,10 +0,0 @@
package view
// EngineFuncer is an addition of a view engine,
// if a view engine implements that interface
// then iris can add some closed-relative iris functions
// like {{ urlpath }} and {{ urlpath }}.
type EngineFuncer interface {
// AddFunc should adds a function to the template's function map.
AddFunc(funcName string, funcBody interface{})
}

View File

@@ -5,6 +5,18 @@ import (
"io"
"path/filepath"
"strings"
"github.com/kataras/iris/v12/context"
)
type (
// Engine is the interface for a compatible Iris view engine.
// It's an alias of context.ViewEngine.
Engine = context.ViewEngine
// EngineFuncer is the interface for a compatible Iris view engine
// which accepts builtin framework functions such as url, urlpath and tr.
// It's an alias of context.ViewEngineFuncer.
EngineFuncer = context.ViewEngineFuncer
)
// View is responsible to
@@ -73,3 +85,19 @@ func (v *View) Load() error {
}
return nil
}
// NoLayout disables the configuration's layout for a specific execution.
const NoLayout = "iris.nolayout"
// returns empty if it's no layout or empty layout and empty configuration's layout.
func getLayout(layout string, globalLayout string) string {
if layout == NoLayout {
return ""
}
if layout == "" && globalLayout != "" {
return globalLayout
}
return layout
}