From 1354b74822b5cde05f9a4009b8667c2fe8c88650 Mon Sep 17 00:00:00 2001 From: Makis Maropoulos Date: Sun, 3 Jul 2016 02:01:48 +0200 Subject: [PATCH] Available is never returns false, just re-allocate on .Close - test https://github.com/iris-contrib/tests/blob/master/server_test.go --- initiatory.go | 4 +- .../template/engine/handlebars/handlebars.go | 2 +- render/template/engine/html/html.go | 2 +- render/template/template.go | 40 ++++++++++--------- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/initiatory.go b/initiatory.go index b5a26ba6..91541762 100644 --- a/initiatory.go +++ b/initiatory.go @@ -26,7 +26,7 @@ var ( Websocket websocket.Server HTTPServer *Server // Available is a channel type of bool, fired to true when the server is opened and all plugins ran - // fires false when .Close is called manually. + // never fires false, if the .Close called then the channel is re-allocating. // the channel is always oepen until you close it when you don't need this. // // Note: it is a simple channel and decided to put it here and no inside HTTPServer, doesn't have statuses just true and false, simple as possible @@ -189,7 +189,7 @@ func (s *Framework) openServer() (err error) { // closeServer is used to close the tcp listener from the server, returns an error func (s *Framework) closeServer() error { s.Plugins.DoPreClose(s) - s.Available <- false + s.Available = make(chan bool) return s.HTTPServer.Close() } diff --git a/render/template/engine/handlebars/handlebars.go b/render/template/engine/handlebars/handlebars.go index 4acf91e2..e1ec963d 100644 --- a/render/template/engine/handlebars/handlebars.go +++ b/render/template/engine/handlebars/handlebars.go @@ -123,7 +123,7 @@ func (e *Engine) ExecuteWriter(out io.Writer, name string, binding interface{}, isLayout := false renderFilename := name - if layout != "" && layout != config.NoLayout { + if layout != "" { isLayout = true renderFilename = layout // the render becomes the layout, and the name is the partial. } diff --git a/render/template/engine/html/html.go b/render/template/engine/html/html.go index d13a3589..f1a4448f 100644 --- a/render/template/engine/html/html.go +++ b/render/template/engine/html/html.go @@ -233,7 +233,7 @@ func (s *Engine) runtimeFuncsFor(name string, binding interface{}) { // ExecuteWriter executes a templates and write its results to the out writer func (s *Engine) ExecuteWriter(out io.Writer, name string, binding interface{}, layout string) error { - if layout != "" && layout != config.NoLayout { + if layout != "" { s.layoutFuncsFor(name, binding) name = layout diff --git a/render/template/template.go b/render/template/template.go index 0ce3db94..644128e1 100644 --- a/render/template/template.go +++ b/render/template/template.go @@ -148,6 +148,25 @@ func RegisterSharedFuncs(theFuncs map[string]interface{}) { } +func (t *Template) getLayout(ctx context.IContext, passedLayouts []string) string { + + _layout := "" + if len(passedLayouts) > 0 { + _layout = passedLayouts[0] + } else if ctx != nil { + if layoutFromCtx := ctx.GetString(config.TemplateLayoutContextKey); layoutFromCtx != "" { + _layout = layoutFromCtx + } + } + if _layout == "" && _layout != config.NoLayout { + if t.Layout != config.NoLayout { // the config's Layout is disabled if "" , config.NoLayout should be passed only on ctx.Render but for any case check if user set it as config.NoLayout also + _layout = t.Layout + } + } + + return _layout +} + // Render renders a template using the context's writer func (t *Template) Render(ctx context.IContext, name string, binding interface{}, layout ...string) (err error) { @@ -163,18 +182,8 @@ func (t *Template) Render(ctx context.IContext, name string, binding interface{} } } - // I don't like this, something feels wrong - _layout := "" - if len(layout) > 0 { - _layout = layout[0] - } else if layoutFromCtx := ctx.GetString(config.TemplateLayoutContextKey); layoutFromCtx != "" { - _layout = layoutFromCtx - } - if _layout == "" { - _layout = t.Layout - } + _layout := t.getLayout(ctx, layout) - // ctx.GetRequestCtx().Response.Header.Set("Content-Type", t.ContentType) var out io.Writer @@ -209,14 +218,7 @@ func (t *Template) RenderString(name string, binding interface{}, layout ...stri } } - // I don't like this, something feels wrong - _layout := "" - if len(layout) > 0 { - _layout = layout[0] - } - if _layout == "" { - _layout = t.Layout - } + _layout := t.getLayout(nil, layout) out := t.buffer.Get() // if we have problems later consider that -> out.Reset()