Exemple #1
0
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
}
Exemple #2
0
// Render renders a template using the context's writer
func (t *Template) Render(ctx context.IContext, name string, binding interface{}, layout ...string) (err error) {

	if t == nil { // No engine was given but .Render was called
		ctx.HTML(403, "<b> Iris </b> <br/> Templates are disabled via config.NoEngine, check your iris' configuration please.")
		return fmt.Errorf("[IRIS TEMPLATES] Templates are disabled via config.NoEngine, check your iris' configuration please.\n")
	}

	// build templates again on each render if IsDevelopment.
	if t.IsDevelopment {
		if err = t.Engine.BuildTemplates(); err != nil {
			return
		}
	}

	// 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
	}

	//
	ctx.GetRequestCtx().Response.Header.Set("Content-Type", t.ContentType)

	var out io.Writer
	if t.Gzip {
		ctx.GetRequestCtx().Response.Header.Add("Content-Encoding", "gzip")
		gzipWriter := t.gzipWriterPool.Get().(*gzip.Writer)
		gzipWriter.Reset(ctx.GetRequestCtx().Response.BodyWriter())
		defer gzipWriter.Close()
		defer t.gzipWriterPool.Put(gzipWriter)
		out = gzipWriter
	} else {
		out = ctx.GetRequestCtx().Response.BodyWriter()
	}

	err = t.Engine.ExecuteWriter(out, name, binding, _layout)

	return
}