@@ -67,8 +67,10 @@ type (
6767 // TemplateExecutor 响应报文模板执行器
6868 TemplateExecutor struct {
6969 IsGolangTemplate bool
70+ RenderHeader bool
7071 IsBinData bool
7172 template * template.Template
73+ headerTemplate * template.Template
7274 header * fasthttp.ResponseHeader
7375 body []byte
7476 }
@@ -270,24 +272,58 @@ func (fe *FilterExecutor) Filter(request *fasthttp.Request) bool {
270272// Render 渲染函数
271273func (te * TemplateExecutor ) Render (ctx * fasthttp.RequestCtx , v map [string ]interface {}, weight map [string ]string ) error {
272274 te .header .CopyTo (& ctx .Response .Header )
275+ rc := & RenderContext {}
276+ if te .RenderHeader {
277+ // 渲染header template
278+ if err := te .handleHeaderTemplate (rc , ctx , v , weight ); err != nil {
279+ return err
280+ }
281+ }
273282 if ! te .IsGolangTemplate {
274283 ctx .Response .SetBody (te .body )
275284 return nil
276285 }
277286
278- // 开始渲染模板
279- var rc RenderContext
287+ // 开始渲染body模板
288+ if rc == nil {
289+ rc .parseParams (ctx , v , weight )
290+ }
291+ return te .template .Execute (ctx .Response .BodyWriter (), rc )
292+ }
293+
294+ // handleHeaderTemplate 处理header中的template
295+ func (te * TemplateExecutor ) handleHeaderTemplate (rc * RenderContext , ctx * fasthttp.RequestCtx , v map [string ]interface {}, weight map [string ]string ) error {
296+ // parse params
297+ rc .parseParams (ctx , v , weight )
298+ // render template
299+ var buf bytes.Buffer
300+ if err := te .headerTemplate .Execute (& buf , rc ); err != nil {
301+ misc .Logger .Error (err .Error ())
302+ return err
303+ }
304+ // merge to ctx.response.header
305+ headerToBeSet := make (map [string ]string )
306+ if err := json .Unmarshal (buf .Bytes (), & headerToBeSet ); err != nil {
307+ misc .Logger .Error (err .Error ())
308+ return err
309+ }
310+ for k , v := range headerToBeSet {
311+ ctx .Response .Header .Set (k , v )
312+ }
313+ return nil
314+ }
315+
316+ // parseParams 解析Request中的参数,供template渲染使用
317+ func (rc * RenderContext ) parseParams (ctx * fasthttp.RequestCtx , v map [string ]interface {}, weight map [string ]string ) {
280318 h := extractHeaderAsParams (& ctx .Request )
281319 q := extractQueryAsParams (& ctx .Request )
282320 f , j := extractBodyAsParams (& ctx .Request )
283-
284321 rc .Variable = v
285322 rc .Weight = weight
286323 rc .Header = h
287324 rc .Query = q
288325 rc .Form = f
289326 rc .Json = j
290- return te .template .Execute (ctx .Response .BodyWriter (), rc )
291327}
292328
293329// Render 渲染函数
@@ -376,6 +412,11 @@ func dateDelta(date, layout string, year, month, day int) string {
376412 return t .AddDate (year , month , day ).Format (layout )
377413}
378414
415+ func HTMLUnescaped (x string ) interface {} {
416+ // do not encode HTML, like "&" --> "&"
417+ return template .HTML (x )
418+ }
419+
379420func init () {
380421 // create build-in template functions
381422 defaultTemplateFuncs = make (template.FuncMap )
@@ -385,4 +426,5 @@ func init() {
385426 _ = RegisterTemplateFunc ("plus" , plus )
386427 _ = RegisterTemplateFunc ("rand_string" , misc .GenRandomString )
387428 _ = RegisterTemplateFunc ("date_delta" , dateDelta )
429+ _ = RegisterTemplateFunc ("html_unescaped" , HTMLUnescaped )
388430}
0 commit comments