Embrace the feature request for per-party layout https://github.com/kataras/iris/issues/180

This commit is contained in:
Makis Maropoulos 2016-06-14 20:29:01 +03:00
parent d837381b16
commit 6a77c2ed22
3 changed files with 29 additions and 4 deletions

View File

@ -31,6 +31,8 @@ const (
// NoLayout to disable layout for a particular template file // NoLayout to disable layout for a particular template file
NoLayout = "@.|.@iris_no_layout@.|.@" NoLayout = "@.|.@iris_no_layout@.|.@"
// TemplateLayoutContextKey is the name of the user values which can be used to set a template layout from a middleware and override the parent's
TemplateLayoutContextKey = "templateLayout"
) )
var ( var (

29
iris.go
View File

@ -108,14 +108,17 @@ type (
MuxAPI interface { MuxAPI interface {
Party(string, ...HandlerFunc) MuxAPI Party(string, ...HandlerFunc) MuxAPI
// middleware
Use(...Handler) Use(...Handler)
UseFunc(...HandlerFunc) UseFunc(...HandlerFunc)
// main handlers
Handle(string, string, ...Handler) RouteNameFunc Handle(string, string, ...Handler) RouteNameFunc
HandleFunc(string, string, ...HandlerFunc) RouteNameFunc HandleFunc(string, string, ...HandlerFunc) RouteNameFunc
// H_ is used to convert a context.IContext handler func to iris.HandlerFunc, is used only inside iris internal package to avoid import cycles // H_ is used to convert a context.IContext handler func to iris.HandlerFunc, is used only inside iris internal package to avoid import cycles
H_(string, string, func(context.IContext)) func(string) H_(string, string, func(context.IContext)) func(string)
API(string, HandlerAPI, ...HandlerFunc) API(string, HandlerAPI, ...HandlerFunc)
// http methods
Get(string, ...HandlerFunc) RouteNameFunc Get(string, ...HandlerFunc) RouteNameFunc
Post(string, ...HandlerFunc) RouteNameFunc Post(string, ...HandlerFunc) RouteNameFunc
Put(string, ...HandlerFunc) RouteNameFunc Put(string, ...HandlerFunc) RouteNameFunc
@ -127,6 +130,7 @@ type (
Trace(string, ...HandlerFunc) RouteNameFunc Trace(string, ...HandlerFunc) RouteNameFunc
Any(string, ...HandlerFunc) Any(string, ...HandlerFunc)
// static content
StaticHandler(string, int, bool, bool, []string) HandlerFunc StaticHandler(string, int, bool, bool, []string) HandlerFunc
Static(string, string, int) RouteNameFunc Static(string, string, int) RouteNameFunc
StaticFS(string, string, int) RouteNameFunc StaticFS(string, string, int) RouteNameFunc
@ -134,6 +138,9 @@ type (
StaticServe(string, ...string) RouteNameFunc StaticServe(string, ...string) RouteNameFunc
StaticContent(string, string, []byte) func(string) StaticContent(string, string, []byte) func(string)
Favicon(string, ...string) RouteNameFunc Favicon(string, ...string) RouteNameFunc
// templates
Layout(string) MuxAPI // returns itself
} }
) )
@ -537,10 +544,6 @@ var (
var _ MuxAPI = &muxAPI{} var _ MuxAPI = &muxAPI{}
func pathIsSubdomain(s string) bool {
return strings.Index(s, subdomainIndicator) != -1
}
// Party is just a group joiner of routes which have the same prefix and share same middleware(s) also. // Party is just a group joiner of routes which have the same prefix and share same middleware(s) also.
// Party can also be named as 'Join' or 'Node' or 'Group' , Party chosen because it has more fun // Party can also be named as 'Join' or 'Node' or 'Group' , Party chosen because it has more fun
func Party(relativePath string, handlersFn ...HandlerFunc) MuxAPI { func Party(relativePath string, handlersFn ...HandlerFunc) MuxAPI {
@ -1229,3 +1232,21 @@ func (api *muxAPI) Favicon(favPath string, requestPath ...string) RouteNameFunc
api.Head(reqPath, h) api.Head(reqPath, h)
return api.Get(reqPath, h) return api.Get(reqPath, h)
} }
// Layout oerrides the parent template layout with a more specific layout for this Party
// returns this Party, to continue as normal
// example:
// my := iris.Party("/my").Layout("layouts/mylayout.html")
// {
// my.Get("/", func(ctx *iris.Context) {
// ctx.MustRender("page1.html", nil)
// })
// }
//
func (api *muxAPI) Layout(tmplLayoutFile string) MuxAPI {
api.UseFunc(func(ctx *Context) {
ctx.Set(config.TemplateLayoutContextKey, tmplLayoutFile)
ctx.Next()
})
return api
}

View File

@ -163,6 +163,8 @@ func (t *Template) Render(ctx context.IContext, name string, binding interface{}
_layout := "" _layout := ""
if len(layout) > 0 { if len(layout) > 0 {
_layout = layout[0] _layout = layout[0]
} else if layoutFromCtx := ctx.GetString(config.TemplateLayoutContextKey); layoutFromCtx != "" {
_layout = layoutFromCtx
} }
if _layout == "" { if _layout == "" {
_layout = t.Layout _layout = t.Layout