Implement the custom {{ url }} to the PongoEngine also

This commit is contained in:
Makis Maropoulos 2016-06-05 02:17:17 +03:00
parent 7f5fd1904b
commit e40fe0a9f3
2 changed files with 55 additions and 20 deletions

View File

@ -4,7 +4,7 @@
- NEW: Wildcard(dynamic) subdomains, read [here](https://kataras.gitbooks.io/iris/content/subdomains.html) - NEW: Wildcard(dynamic) subdomains, read [here](https://kataras.gitbooks.io/iris/content/subdomains.html)
- NEW: Implement feature request [#165](https://github.com/kataras/iris/issues/165). Routes can now be selected by `a custom name`, and this allows us to use the {{ url "custom-name" "any" "named" "parameters"}}`` inside HTMLEngine's templates. Example [here](https://github.com/iris-contrib/examples/tree/master/templates_9). - NEW: Implement feature request [#165](https://github.com/kataras/iris/issues/165). Routes can now be selected by `a custom name`, and this allows us to use the {{ url "custom-name" "any" "named" "parameters"}}``: For HTML & Amber engines, example [here](https://github.com/iris-contrib/examples/tree/master/templates_9). For PongoEngine, example [here](https://github.com/iris-contrib/examples/tree/master/templates_10_pongo)
- Remove the [x/net/context](https://godoc.org/golang.org/x/net/context), it has been useless after v2. - Remove the [x/net/context](https://godoc.org/golang.org/x/net/context), it has been useless after v2.

73
iris.go
View File

@ -5,14 +5,13 @@ package iris
import ( import (
"os" "os"
"sync"
"strconv" "strconv"
"sync"
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/flosch/pongo2"
"github.com/kataras/iris/config" "github.com/kataras/iris/config"
"github.com/kataras/iris/logger" "github.com/kataras/iris/logger"
"github.com/kataras/iris/mail" "github.com/kataras/iris/mail"
@ -119,7 +118,9 @@ func (s *Iris) initTemplates() {
// init the templates // init the templates
// set the custom iris-direct-integration functions, layout and no-layout if HTMLEngine is used // set the custom iris-direct-integration functions, layout and no-layout if HTMLEngine is used
if s.config.Render.Template.Engine == config.HTMLEngine { templateConfig := &s.config.Render.Template
///TODO gia AMber episis
if templateConfig.Engine == config.HTMLEngine || templateConfig.Engine == config.AmberEngine {
funcs := map[string]interface{}{ funcs := map[string]interface{}{
"url": func(routeName string, args ...interface{}) (string, error) { "url": func(routeName string, args ...interface{}) (string, error) {
r := s.RouteByName(routeName) r := s.RouteByName(routeName)
@ -132,28 +133,62 @@ func (s *Iris) initTemplates() {
}, },
} }
// these should be already a non-nil map but if .New(cfg) it's not, is mergo's bug, temporary: // these should be already a non-nil map but if .New(cfg) it's not, is mergo's bug, temporary:
if s.config.Render.Template.HTMLTemplate.LayoutFuncs == nil { if templateConfig.Engine == config.HTMLEngine {
s.config.Render.Template.HTMLTemplate.LayoutFuncs = make(map[string]interface{}, 1) if templateConfig.HTMLTemplate.LayoutFuncs == nil {
templateConfig.HTMLTemplate.LayoutFuncs = make(map[string]interface{}, 1)
}
if templateConfig.HTMLTemplate.Funcs == nil {
templateConfig.HTMLTemplate.Funcs = make(map[string]interface{}, 1)
}
for k, v := range funcs {
// we don't want to override the user's LayoutFuncs, user should be able to override anything.
if templateConfig.HTMLTemplate.LayoutFuncs[k] == nil {
templateConfig.HTMLTemplate.LayoutFuncs[k] = v
}
if templateConfig.HTMLTemplate.Funcs[k] == nil {
templateConfig.HTMLTemplate.Funcs[k] = v
}
}
} else if templateConfig.Engine == config.AmberEngine {
if templateConfig.Amber.Funcs == nil {
templateConfig.Amber.Funcs = make(map[string]interface{}, 1)
}
for k, v := range funcs {
if templateConfig.Amber.Funcs[k] == nil {
templateConfig.Amber.Funcs[k] = v
}
}
} }
if s.config.Render.Template.HTMLTemplate.Funcs == nil {
s.config.Render.Template.HTMLTemplate.Funcs = make(map[string]interface{}, 1)
}
// //
for k, v := range funcs { } else if templateConfig.Engine == config.PongoEngine {
// we don't want to override the user's LayoutFuncs, user should be able to override anything. if templateConfig.Pongo.Filters == nil {
if s.config.Render.Template.HTMLTemplate.LayoutFuncs[k] == nil { templateConfig.Pongo.Filters = make(map[string]pongo2.FilterFunction, 1)
s.config.Render.Template.HTMLTemplate.LayoutFuncs[k] = v
}
if s.config.Render.Template.HTMLTemplate.Funcs[k] == nil {
s.config.Render.Template.HTMLTemplate.Funcs[k] = v
}
} }
urlFunc := func(routeName string, args ...interface{}) (out *pongo2.Value) {
r := s.RouteByName(routeName)
// check if not found
if r.GetMethod() == "" {
return pongo2.AsValue(ErrRenderRouteNotFound.Format(routeName).Error())
}
return pongo2.AsValue(r.ParseURI(args...))
}
// register it to the global context, no as Filter.
templateConfig.Pongo.Globals["url"] = urlFunc
} }
s.templates = template.New(s.config.Render.Template) s.templates = template.New(s.config.Render.Template)