...

Source file src/github.com/gin-gonic/gin/render/html.go

Documentation: github.com/gin-gonic/gin/render

     1  // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
     2  // Use of this source code is governed by a MIT style
     3  // license that can be found in the LICENSE file.
     4  
     5  package render
     6  
     7  import (
     8  	"html/template"
     9  	"net/http"
    10  )
    11  
    12  // Delims represents a set of Left and Right delimiters for HTML template rendering.
    13  type Delims struct {
    14  	// Left delimiter, defaults to {{.
    15  	Left string
    16  	// Right delimiter, defaults to }}.
    17  	Right string
    18  }
    19  
    20  // HTMLRender interface is to be implemented by HTMLProduction and HTMLDebug.
    21  type HTMLRender interface {
    22  	// Instance returns an HTML instance.
    23  	Instance(string, any) Render
    24  }
    25  
    26  // HTMLProduction contains template reference and its delims.
    27  type HTMLProduction struct {
    28  	Template *template.Template
    29  	Delims   Delims
    30  }
    31  
    32  // HTMLDebug contains template delims and pattern and function with file list.
    33  type HTMLDebug struct {
    34  	Files   []string
    35  	Glob    string
    36  	Delims  Delims
    37  	FuncMap template.FuncMap
    38  }
    39  
    40  // HTML contains template reference and its name with given interface object.
    41  type HTML struct {
    42  	Template *template.Template
    43  	Name     string
    44  	Data     any
    45  }
    46  
    47  var htmlContentType = []string{"text/html; charset=utf-8"}
    48  
    49  // Instance (HTMLProduction) returns an HTML instance which it realizes Render interface.
    50  func (r HTMLProduction) Instance(name string, data any) Render {
    51  	return HTML{
    52  		Template: r.Template,
    53  		Name:     name,
    54  		Data:     data,
    55  	}
    56  }
    57  
    58  // Instance (HTMLDebug) returns an HTML instance which it realizes Render interface.
    59  func (r HTMLDebug) Instance(name string, data any) Render {
    60  	return HTML{
    61  		Template: r.loadTemplate(),
    62  		Name:     name,
    63  		Data:     data,
    64  	}
    65  }
    66  func (r HTMLDebug) loadTemplate() *template.Template {
    67  	if r.FuncMap == nil {
    68  		r.FuncMap = template.FuncMap{}
    69  	}
    70  	if len(r.Files) > 0 {
    71  		return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFiles(r.Files...))
    72  	}
    73  	if r.Glob != "" {
    74  		return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob))
    75  	}
    76  	panic("the HTML debug render was created without files or glob pattern")
    77  }
    78  
    79  // Render (HTML) executes template and writes its result with custom ContentType for response.
    80  func (r HTML) Render(w http.ResponseWriter) error {
    81  	r.WriteContentType(w)
    82  
    83  	if r.Name == "" {
    84  		return r.Template.Execute(w, r.Data)
    85  	}
    86  	return r.Template.ExecuteTemplate(w, r.Name, r.Data)
    87  }
    88  
    89  // WriteContentType (HTML) writes HTML ContentType.
    90  func (r HTML) WriteContentType(w http.ResponseWriter) {
    91  	writeContentType(w, htmlContentType)
    92  }
    93  

View as plain text