From 36dce1a73f35c3f94236a759fe322a9fef5ec945 Mon Sep 17 00:00:00 2001 From: SCys Date: Mon, 9 Nov 2015 00:24:18 +0800 Subject: [PATCH] + support custom render, pongo2 tested --- context.go | 5 +++++ gin.go | 19 ++++++++++++------- render/custom_render.go | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 render/custom_render.go diff --git a/context.go b/context.go index b784c14b..0ee1515c 100644 --- a/context.go +++ b/context.go @@ -341,6 +341,11 @@ func (c *Context) HTML(code int, name string, obj interface{}) { c.Render(code, instance) } +func (c *Context) CustomRender(code int, name string, obj interface{}) { + instance := c.engine.CustomRender.Instance(name, obj) + c.Render(code, instance) +} + // IndentedJSON serializes the given struct as pretty JSON (indented + endlines) into the response body. // It also sets the Content-Type as "application/json". // WARNING: we recommend to use this only for development propuses since printing pretty JSON is diff --git a/gin.go b/gin.go index 3834d67e..e5a6712e 100644 --- a/gin.go +++ b/gin.go @@ -44,13 +44,14 @@ type ( // Create an instance of Engine, by using New() or Default() Engine struct { RouterGroup - HTMLRender render.HTMLRender - allNoRoute HandlersChain - allNoMethod HandlersChain - noRoute HandlersChain - noMethod HandlersChain - pool sync.Pool - trees methodTrees + HTMLRender render.HTMLRender + CustomRender render.CustomRender + allNoRoute HandlersChain + allNoMethod HandlersChain + noRoute HandlersChain + noMethod HandlersChain + pool sync.Pool + trees methodTrees // Enables automatic redirection if the current route can't be matched but a // handler for the path with (without) the trailing slash exists. @@ -147,6 +148,10 @@ func (engine *Engine) SetHTMLTemplate(templ *template.Template) { engine.HTMLRender = render.HTMLProduction{Template: templ} } +func (engine *Engine) SetCustomRenderFunc(f render.CustomRenderFunc) { + engine.CustomRender = render.CustomRenderProduction{f} +} + // Adds handlers for NoRoute. It return a 404 code by default. func (engine *Engine) NoRoute(handlers ...HandlerFunc) { engine.noRoute = handlers diff --git a/render/custom_render.go b/render/custom_render.go new file mode 100644 index 00000000..cce11905 --- /dev/null +++ b/render/custom_render.go @@ -0,0 +1,39 @@ +// Copyright 2014 Manu Martinez-Almeida. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package render + +import ( + "net/http" +) + +type ( + CustomRenderFunc func(http.ResponseWriter, string, interface{}) error + + CustomRender interface { + Instance(string, interface{}) Render + } + + CustomRenderProduction struct { + RenderFunc CustomRenderFunc + } + + Custom struct { + RenderFunc CustomRenderFunc + Name string + Data interface{} + } +) + +func (r CustomRenderProduction) Instance(name string, data interface{}) Render { + return Custom{ + RenderFunc: r.RenderFunc, + Name: name, + Data: data, + } +} + +func (r Custom) Render(w http.ResponseWriter) error { + return r.RenderFunc(w, r.Name, r.Data) +}