From e40fe0a9f3c1e836d16e0b6bb6af336e81d2eb45 Mon Sep 17 00:00:00 2001 From: Makis Maropoulos Date: Sun, 5 Jun 2016 02:17:17 +0300 Subject: [PATCH] Implement the custom {{ url }} to the PongoEngine also --- HISTORY.md | 2 +- iris.go | 73 ++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index a3f348ce..f155fe7a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -4,7 +4,7 @@ - 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. diff --git a/iris.go b/iris.go index 98bd2415..76cab2c7 100644 --- a/iris.go +++ b/iris.go @@ -5,14 +5,13 @@ package iris import ( "os" - - "sync" - "strconv" + "sync" "sync/atomic" "time" "github.com/fatih/color" + "github.com/flosch/pongo2" "github.com/kataras/iris/config" "github.com/kataras/iris/logger" "github.com/kataras/iris/mail" @@ -119,7 +118,9 @@ func (s *Iris) initTemplates() { // init the templates // 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{}{ "url": func(routeName string, args ...interface{}) (string, error) { 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: - if s.config.Render.Template.HTMLTemplate.LayoutFuncs == nil { - s.config.Render.Template.HTMLTemplate.LayoutFuncs = make(map[string]interface{}, 1) + if templateConfig.Engine == config.HTMLEngine { + 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 { - // we don't want to override the user's LayoutFuncs, user should be able to override anything. - if s.config.Render.Template.HTMLTemplate.LayoutFuncs[k] == nil { - 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 - } - + } else if templateConfig.Engine == config.PongoEngine { + if templateConfig.Pongo.Filters == nil { + templateConfig.Pongo.Filters = make(map[string]pongo2.FilterFunction, 1) } + 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)