@@ -134,6 +134,15 @@ type HTMLOptions struct {
134134 Layout string
135135 // Funcs added to Options.Funcs.
136136 Funcs template.FuncMap
137+ // ContentType overrides Options.HTMLContentType for this call only. Charset is not appended.
138+ ContentType string
139+ }
140+
141+ // CallOptions is a struct for overriding rendering Options on a per-call basis.
142+ type CallOptions struct {
143+ // ContentType overrides the instance-level content type for this call only.
144+ // Charset is not appended.
145+ ContentType string
137146}
138147
139148// Render is a service that provides functions for easily writing JSON, XML,
@@ -442,6 +451,7 @@ func (r *Render) layoutFuncs(templates *template.Template, name string, binding
442451func (r * Render ) prepareHTMLOptions (htmlOpt []HTMLOptions ) HTMLOptions {
443452 layout := r .opt .Layout
444453 funcs := template.FuncMap {}
454+ var contentType string
445455
446456 for _ , tmp := range r .opt .Funcs {
447457 for k , v := range tmp {
@@ -458,11 +468,14 @@ func (r *Render) prepareHTMLOptions(htmlOpt []HTMLOptions) HTMLOptions {
458468 for k , v := range opt .Funcs {
459469 funcs [k ] = v
460470 }
471+
472+ contentType = opt .ContentType
461473 }
462474
463475 return HTMLOptions {
464- Layout : layout ,
465- Funcs : funcs ,
476+ Layout : layout ,
477+ Funcs : funcs ,
478+ ContentType : contentType ,
466479 }
467480}
468481
@@ -476,10 +489,18 @@ func (r *Render) Render(w io.Writer, e Engine, data interface{}) error {
476489 return err
477490}
478491
492+ // resolveContentType returns the override ContentType if one was provided, otherwise the default.
493+ func resolveContentType (defaultCT string , opts []CallOptions ) string {
494+ if len (opts ) > 0 && opts [0 ].ContentType != "" {
495+ return opts [0 ].ContentType
496+ }
497+ return defaultCT
498+ }
499+
479500// Data writes out the raw bytes as binary data.
480- func (r * Render ) Data (w io.Writer , status int , v []byte ) error {
501+ func (r * Render ) Data (w io.Writer , status int , v []byte , opts ... CallOptions ) error {
481502 head := Head {
482- ContentType : r .opt .BinaryContentType ,
503+ ContentType : resolveContentType ( r .opt .BinaryContentType , opts ) ,
483504 Status : status ,
484505 }
485506
@@ -515,8 +536,13 @@ func (r *Render) HTML(w io.Writer, status int, name string, binding interface{},
515536 }
516537 }
517538
539+ ct := r .opt .HTMLContentType + r .compiledCharset
540+ if opt .ContentType != "" {
541+ ct = opt .ContentType
542+ }
543+
518544 head := Head {
519- ContentType : r . opt . HTMLContentType + r . compiledCharset ,
545+ ContentType : ct ,
520546 Status : status ,
521547 }
522548
@@ -531,9 +557,9 @@ func (r *Render) HTML(w io.Writer, status int, name string, binding interface{},
531557}
532558
533559// JSON marshals the given interface object and writes the JSON response.
534- func (r * Render ) JSON (w io.Writer , status int , v interface {}) error {
560+ func (r * Render ) JSON (w io.Writer , status int , v interface {}, opts ... CallOptions ) error {
535561 head := Head {
536- ContentType : r .opt .JSONContentType + r .compiledCharset ,
562+ ContentType : resolveContentType ( r .opt .JSONContentType + r .compiledCharset , opts ) ,
537563 Status : status ,
538564 }
539565
@@ -550,9 +576,9 @@ func (r *Render) JSON(w io.Writer, status int, v interface{}) error {
550576}
551577
552578// JSONP marshals the given interface object and writes the JSON response.
553- func (r * Render ) JSONP (w io.Writer , status int , callback string , v interface {}) error {
579+ func (r * Render ) JSONP (w io.Writer , status int , callback string , v interface {}, opts ... CallOptions ) error {
554580 head := Head {
555- ContentType : r .opt .JSONPContentType + r .compiledCharset ,
581+ ContentType : resolveContentType ( r .opt .JSONPContentType + r .compiledCharset , opts ) ,
556582 Status : status ,
557583 }
558584
@@ -566,9 +592,9 @@ func (r *Render) JSONP(w io.Writer, status int, callback string, v interface{})
566592}
567593
568594// Text writes out a string as plain text.
569- func (r * Render ) Text (w io.Writer , status int , v string ) error {
595+ func (r * Render ) Text (w io.Writer , status int , v string , opts ... CallOptions ) error {
570596 head := Head {
571- ContentType : r .opt .TextContentType + r .compiledCharset ,
597+ ContentType : resolveContentType ( r .opt .TextContentType + r .compiledCharset , opts ) ,
572598 Status : status ,
573599 }
574600
@@ -580,9 +606,9 @@ func (r *Render) Text(w io.Writer, status int, v string) error {
580606}
581607
582608// XML marshals the given interface object and writes the XML response.
583- func (r * Render ) XML (w io.Writer , status int , v interface {}) error {
609+ func (r * Render ) XML (w io.Writer , status int , v interface {}, opts ... CallOptions ) error {
584610 head := Head {
585- ContentType : r .opt .XMLContentType + r .compiledCharset ,
611+ ContentType : resolveContentType ( r .opt .XMLContentType + r .compiledCharset , opts ) ,
586612 Status : status ,
587613 }
588614
0 commit comments