以上常量声明了内容类型常量以及本包支持的各种内容类型MIME值。以及各种具体内容类型相关的常量,比如字符编码方式、JSONP前缀、XML前缀,模版左右分割符等等一些常量。
类型声明部分
这部分声明了如下类型:
-
M: 映射类型,描述代表用于发送的响应数据便捷类型。
Options: 描述选项类型。
Render: 用于描述renderer类型。
type (
// M describes handy type that represents data to send as response
M map[string]interface{}
// Options describes an option type
Options struct {
// Charset represents the Response charset; default: utf-8
Charset string
// ContentJSON represents the Content-Type for JSON
ContentJSON string
// ContentJSONP represents the Content-Type for JSONP
ContentJSONP string
// ContentXML represents the Content-Type for XML
ContentXML string
// ContentYAML represents the Content-Type for YAML
ContentYAML string
// ContentHTML represents the Content-Type for HTML
ContentHTML string
// ContentText represents the Content-Type for Text
ContentText string
// ContentBinary represents the Content-Type for octet-stream
ContentBinary string
// UnEscapeHTML set UnEscapeHTML for JSON; default false
UnEscapeHTML bool
// DisableCharset set DisableCharset in Response Content-Type
DisableCharset bool
// Debug set the debug mode. if debug is true then every time "VIEW" call parse the templates
Debug bool
// JSONIndent set JSON Indent in response; default false
JSONIndent bool
// XMLIndent set XML Indent in response; default false
XMLIndent bool
// JSONPrefix set Prefix in JSON response
JSONPrefix string
// XMLPrefix set Prefix in XML response
XMLPrefix string
// TemplateDir set the Template directory
TemplateDir string
// TemplateExtension set the Template extension
TemplateExtension string
// LeftDelim set template left delimiter default is {{
LeftDelim string
// RightDelim set template right delimiter default is }}
RightDelim string
// LayoutExtension set the Layout extension
LayoutExtension string
// FuncMap contain function map for template
FuncMap []template.FuncMap
// ParseGlobPattern contain parse glob pattern
ParseGlobPattern string
}
// Render describes a renderer type
Render struct {
opts Options
templates map[string]*template.Template
globTemplates *template.Template
headers map[string]string
}
)
New函数
// New return a new instance of a pointer to Render
func New(opts ...Options) *Render {
var opt Options
if opts != nil {
opt = opts[0]
}
r := &Render{
opts: opt,
templates: make(map[string]*template.Template),
}
// build options for the Render instance
r.buildOptions()
// if TemplateDir is not empty then call the parseTemplates
if r.opts.TemplateDir != "" {
r.parseTemplates()
}
// ParseGlobPattern is not empty then parse template with pattern
if r.opts.ParseGlobPattern != "" {
r.parseGlob()
}
return r
}










