mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
Implement the custom {{ url }} to the PongoEngine also
This commit is contained in:
parent
7f5fd1904b
commit
e40fe0a9f3
|
@ -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.
|
||||||
|
|
||||||
|
|
61
iris.go
61
iris.go
|
@ -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 s.config.Render.Template.HTMLTemplate.Funcs == nil {
|
if templateConfig.HTMLTemplate.Funcs == nil {
|
||||||
s.config.Render.Template.HTMLTemplate.Funcs = make(map[string]interface{}, 1)
|
templateConfig.HTMLTemplate.Funcs = make(map[string]interface{}, 1)
|
||||||
}
|
}
|
||||||
//
|
|
||||||
|
|
||||||
for k, v := range funcs {
|
for k, v := range funcs {
|
||||||
// we don't want to override the user's LayoutFuncs, user should be able to override anything.
|
// 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 {
|
if templateConfig.HTMLTemplate.LayoutFuncs[k] == nil {
|
||||||
s.config.Render.Template.HTMLTemplate.LayoutFuncs[k] = v
|
templateConfig.HTMLTemplate.LayoutFuncs[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.config.Render.Template.HTMLTemplate.Funcs[k] == nil {
|
if templateConfig.HTMLTemplate.Funcs[k] == nil {
|
||||||
s.config.Render.Template.HTMLTemplate.Funcs[k] = v
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
} 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)
|
s.templates = template.New(s.config.Render.Template)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user