1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-23 11:56:00 +00:00

New: i18n pluralization and variables support and more...

fixes: #1649, #1648, #1641, #1650

relative to: #1597
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-09-29 19:19:19 +03:00
parent f224ded740
commit 4065819688
63 changed files with 2054 additions and 684 deletions

View File

@@ -19,6 +19,7 @@ import (
// Ace(AssetFile(), ".ace") for embedded data.
func Ace(fs interface{}, extension string) *HTMLEngine {
s := HTML(fs, extension)
s.name = "Ace"
funcs := make(map[string]interface{}, 0)
@@ -59,5 +60,6 @@ func Ace(fs interface{}, extension string) *HTMLEngine {
return t.Lookup(name).Tree.Root.String(), nil
}
return s
}

View File

@@ -63,7 +63,13 @@ func (s *AmberEngine) RootDir(root string) *AmberEngine {
return s
}
// Name returns the amber engine's name.
func (s *AmberEngine) Name() string {
return "Amber"
}
// Ext returns the file extension which this view engine is responsible to render.
// If the filename extension on ExecuteWriter is empty then this is appended.
func (s *AmberEngine) Ext() string {
return s.extension
}

View File

@@ -48,6 +48,11 @@ func Blocks(fs interface{}, extension string) *BlocksEngine {
return WrapBlocks(blocks.New(fs).Extension(extension))
}
// Name returns the blocks engine's name.
func (s *BlocksEngine) Name() string {
return "Blocks"
}
// RootDir sets the directory to use as the root one inside the provided File System.
func (s *BlocksEngine) RootDir(root string) *BlocksEngine {
s.Engine.RootDir(root)

View File

@@ -136,7 +136,13 @@ func (s *DjangoEngine) RootDir(root string) *DjangoEngine {
return s
}
// Name returns the django engine's name.
func (s *DjangoEngine) Name() string {
return "Django"
}
// Ext returns the file extension which this view engine is responsible to render.
// If the filename extension on ExecuteWriter is empty then this is appended.
func (s *DjangoEngine) Ext() string {
return s.extension
}

View File

@@ -69,7 +69,13 @@ func (s *HandlebarsEngine) RootDir(root string) *HandlebarsEngine {
return s
}
// Name returns the handlebars engine's name.
func (s *HandlebarsEngine) Name() string {
return "Handlebars"
}
// Ext returns the file extension which this view engine is responsible to render.
// If the filename extension on ExecuteWriter is empty then this is appended.
func (s *HandlebarsEngine) Ext() string {
return s.extension
}

View File

@@ -14,6 +14,7 @@ import (
// HTMLEngine contains the html view engine structure.
type HTMLEngine struct {
name string // the view engine's name, can be HTML, Ace or Pug.
// the file system to load from.
fs http.FileSystem
// files configuration
@@ -81,6 +82,7 @@ var emptyFuncs = template.FuncMap{
// HTML(AssetFile(), ".html") for embedded data.
func HTML(fs interface{}, extension string) *HTMLEngine {
s := &HTMLEngine{
name: "HTML",
fs: getFS(fs),
rootDir: "/",
extension: extension,
@@ -102,7 +104,13 @@ func (s *HTMLEngine) RootDir(root string) *HTMLEngine {
return s
}
// Name returns the engine's name.
func (s *HTMLEngine) Name() string {
return s.name
}
// Ext returns the file extension which this view engine is responsible to render.
// If the filename extension on ExecuteWriter is empty then this is appended.
func (s *HTMLEngine) Ext() string {
return s.extension
}
@@ -409,7 +417,7 @@ func (s *HTMLEngine) ExecuteWriter(w io.Writer, name string, layout string, bind
if layout = getLayout(layout, s.layout); layout != "" {
lt := s.Templates.Lookup(layout)
if lt == nil {
return ErrNotExist{name, true}
return ErrNotExist{layout, true}
}
s.layoutFuncsFor(lt, name, bindingData)

View File

@@ -93,7 +93,13 @@ func (s *JetEngine) RootDir(root string) *JetEngine {
return s
}
// Name returns the jet engine's name.
func (s *JetEngine) Name() string {
return "Jet"
}
// Ext should return the final file extension which this view engine is responsible to render.
// If the filename extension on ExecuteWriter is empty then this is appended.
func (s *JetEngine) Ext() string {
return s.extension
}

View File

@@ -25,6 +25,7 @@ import (
// https://github.com/kataras/iris/tree/master/_examples/view/template_pug_3
func Pug(fs interface{}, extension string) *HTMLEngine {
s := HTML(fs, extension)
s.name = "Pug"
s.middleware = func(name string, text []byte) (contents string, err error) {
tmpl := jade.New(name)

View File

@@ -3,10 +3,11 @@ package view
import (
"fmt"
"io"
"path/filepath"
"strings"
"github.com/kataras/iris/v12/context"
"github.com/kataras/golog"
)
type (
@@ -34,71 +35,65 @@ func (e ErrNotExist) Error() string {
return fmt.Sprintf("%s '%s' does not exist", title, e.Name)
}
// View is responsible to
// load the correct templates
// for each of the registered view engines.
type View struct {
engines []Engine
}
// View is just a wrapper on top of the registered template engine.
type View struct{ Engine }
// Register registers a view engine.
func (v *View) Register(e Engine) {
v.engines = append(v.engines, e)
if v.Engine != nil {
golog.Warnf("Engine already exists, replacing the old %q with the new one %q", v.Engine.Name(), e.Name())
}
v.Engine = e
}
// Find receives a filename, gets its extension and returns the view engine responsible for that file extension
func (v *View) Find(filename string) Engine {
// Read-Only no locks needed, at serve/runtime-time the library is not supposed to add new view engines
for i, n := 0, len(v.engines); i < n; i++ {
e := v.engines[i]
if strings.HasSuffix(filename, e.Ext()) {
return e
// Registered reports whether an engine was registered.
func (v *View) Registered() bool {
return v.Engine != nil
}
func (v *View) ensureTemplateName(s string) string {
if s == "" || s == NoLayout {
return s
}
s = strings.TrimPrefix(s, "/")
if ext := v.Engine.Ext(); ext != "" {
if !strings.HasSuffix(s, ext) {
return s + ext
}
}
return nil
}
// Len returns the length of view engines registered so far.
func (v *View) Len() int {
return len(v.engines)
return s
}
// ExecuteWriter calls the correct view Engine's ExecuteWriter func
func (v *View) ExecuteWriter(w io.Writer, filename string, layout string, bindingData interface{}) error {
if len(filename) > 2 {
if filename[0] == '/' { // omit first slash
filename = filename[1:]
}
}
filename = v.ensureTemplateName(filename)
layout = v.ensureTemplateName(layout)
e := v.Find(filename)
if e == nil {
return fmt.Errorf("no view engine found for '%s'", filepath.Ext(filename))
}
return e.ExecuteWriter(w, filename, layout, bindingData)
return v.Engine.ExecuteWriter(w, filename, layout, bindingData)
}
// AddFunc adds a function to all registered engines.
// Each template engine that supports functions has its own AddFunc too.
func (v *View) AddFunc(funcName string, funcBody interface{}) {
for i, n := 0, len(v.engines); i < n; i++ {
e := v.engines[i]
if engineFuncer, ok := e.(EngineFuncer); ok {
engineFuncer.AddFunc(funcName, funcBody)
}
if !v.Registered() {
return
}
if e, ok := v.Engine.(EngineFuncer); ok {
e.AddFunc(funcName, funcBody)
}
}
// Load compiles all the registered engines.
func (v *View) Load() error {
for i, n := 0, len(v.engines); i < n; i++ {
e := v.engines[i]
if err := e.Load(); err != nil {
return err
}
if !v.Registered() {
return fmt.Errorf("No engine is registered")
}
return nil
return v.Engine.Load()
}
// NoLayout disables the configuration's layout for a specific execution.