mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
5e1ac31d13
Former-commit-id: da2423082a10392910bbafcd7683c1c93d7aa556 [formerly 708ea8f31cde627a1b64a5a49c80b5faf571740e] [formerly f127dbccd73fbba2526cb7763c1fafccc926596c [formerly c8f595b2715bfd2ab8a3ad046d05f79e7b632694 [formerly4558c48cdf
]]] Former-commit-id: c9d4a6c430c6c5f731e755f270bdf66f89eaa646 [formerly 291db3593d93e63d7234d85f49773ec34c867f1e] Former-commit-id: d1a21b94fe3760e037c7c02a56ca933123a8cae7 Former-commit-id:9ad5907b48
72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
// 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 (
|
|
"html/template"
|
|
"net/http"
|
|
)
|
|
|
|
type (
|
|
HTMLRender interface {
|
|
Instance(string, interface{}) Render
|
|
}
|
|
|
|
HTMLProduction struct {
|
|
Template *template.Template
|
|
}
|
|
|
|
HTMLDebug struct {
|
|
Files []string
|
|
Glob string
|
|
}
|
|
|
|
HTML struct {
|
|
Template *template.Template
|
|
Name string
|
|
Data interface{}
|
|
}
|
|
)
|
|
|
|
var htmlContentType = []string{"text/html; charset=utf-8"}
|
|
|
|
func (r HTMLProduction) Instance(name string, data interface{}) Render {
|
|
return HTML{
|
|
Template: r.Template,
|
|
Name: name,
|
|
Data: data,
|
|
}
|
|
}
|
|
|
|
func (r HTMLDebug) Instance(name string, data interface{}) Render {
|
|
return HTML{
|
|
Template: r.loadTemplate(),
|
|
Name: name,
|
|
Data: data,
|
|
}
|
|
}
|
|
func (r HTMLDebug) loadTemplate() *template.Template {
|
|
if len(r.Files) > 0 {
|
|
return template.Must(template.ParseFiles(r.Files...))
|
|
}
|
|
if len(r.Glob) > 0 {
|
|
return template.Must(template.ParseGlob(r.Glob))
|
|
}
|
|
panic("the HTML debug render was created without files or glob pattern")
|
|
}
|
|
|
|
func (r HTML) Render(w http.ResponseWriter) error {
|
|
r.WriteContentType(w)
|
|
|
|
if len(r.Name) == 0 {
|
|
return r.Template.Execute(w, r.Data)
|
|
}
|
|
return r.Template.ExecuteTemplate(w, r.Name, r.Data)
|
|
}
|
|
|
|
func (r HTML) WriteContentType(w http.ResponseWriter) {
|
|
writeContentType(w, htmlContentType)
|
|
}
|