html.go
This file
Path
render/html.go
107 lines · 3.0 KiB · Go
Last changed by
fix(context): handle missing HTML renderer (#4805)Langning Zhangbefore adoption5c6a15f
History of this fileTake it
The walk stopped after 200 commits: lines older than cf32d2d point at it.
chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 1–7why, one step back1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package render67import (fix(context): handle missing HTML renderer (#4805)Langning Zhangbefore adoption5c6a15f
line 8why, one step back8 "errors"chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 9–53why, one step back9 "html/template"10 "net/http"1112 "github.com/gin-gonic/gin/internal/fs"13)1415// Delims represents a set of Left and Right delimiters for HTML template rendering.16type Delims struct {17 // Left delimiter, defaults to {{.18 Left string19 // Right delimiter, defaults to }}.20 Right string21}2223// HTMLRender interface is to be implemented by HTMLProduction and HTMLDebug.24type HTMLRender interface {25 // Instance returns an HTML instance.26 Instance(string, any) Render27}2829// HTMLProduction contains template reference and its delims.30type HTMLProduction struct {31 Template *template.Template32 Delims Delims33}3435// HTMLDebug contains template delims and pattern and function with file list.36type HTMLDebug struct {37 Files []string38 Glob string39 FileSystem http.FileSystem40 Patterns []string41 Delims Delims42 FuncMap template.FuncMap43}4445// HTML contains template reference and its name with given interface object.46type HTML struct {47 Template *template.Template48 Name string49 Data any50}5152var htmlContentType = []string{"text/html; charset=utf-8"}53fix(context): handle missing HTML renderer (#4805)Langning Zhangbefore adoption5c6a15f
lines 54–55why, one step back54var errHTMLRendererNotConfigured = errors.New("html renderer is not configured")55chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 56–72why, one step back56// Instance (HTMLProduction) returns an HTML instance which it realizes Render interface.57func (r HTMLProduction) Instance(name string, data any) Render {58 return HTML{59 Template: r.Template,60 Name: name,61 Data: data,62 }63}6465// Instance (HTMLDebug) returns an HTML instance which it realizes Render interface.66func (r HTMLDebug) Instance(name string, data any) Render {67 return HTML{68 Template: r.loadTemplate(),69 Name: name,70 Data: data,71 }72}ci(golangci-lint): update configuration and fix lint issues (#4247)Flc゛before adoptionc4287b1
line 73why, one step back73chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 74–93why, one step back74func (r HTMLDebug) loadTemplate() *template.Template {75 if r.FuncMap == nil {76 r.FuncMap = template.FuncMap{}77 }78 if len(r.Files) > 0 {79 return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFiles(r.Files...))80 }81 if r.Glob != "" {82 return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob))83 }84 if r.FileSystem != nil && len(r.Patterns) > 0 {85 return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFS(86 fs.FileSystem{FileSystem: r.FileSystem}, r.Patterns...))87 }88 panic("the HTML debug render was created without files or glob pattern or file system with patterns")89}9091// Render (HTML) executes template and writes its result with custom ContentType for response.92func (r HTML) Render(w http.ResponseWriter) error {93 r.WriteContentType(w)fix(context): handle missing HTML renderer (#4805)Langning Zhangbefore adoption5c6a15f
lines 94–96why, one step back94 if r.Template == nil {95 return errHTMLRendererNotConfigured96 }chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 97–107why, one step back9798 if r.Name == "" {99 return r.Template.Execute(w, r.Data)100 }101 return r.Template.ExecuteTemplate(w, r.Name, r.Data)102}103104// WriteContentType (HTML) writes HTML ContentType.105func (r HTML) WriteContentType(w http.ResponseWriter) {106 writeContentType(w, htmlContentType)107}