2016-05-30 16:08:09 +02:00
package config
import (
"html/template"
"github.com/flosch/pongo2"
"github.com/imdario/mergo"
)
const (
2016-05-31 10:05:42 +02:00
// NoEngine is a Template's config for engine type
// when use this, the templates are disabled
NoEngine EngineType = - 1
// HTMLEngine is a Template's config for engine type
// when use this, the templates are html/template
HTMLEngine EngineType = 0
// PongoEngine is a Template's config for engine type
// when use this, the templates are flosch/pongo2
PongoEngine EngineType = 1
// MarkdownEngine is a Template's config for engine type
// when use this, the templates are .md files
2016-05-30 16:08:09 +02:00
MarkdownEngine EngineType = 2
2016-05-31 10:05:42 +02:00
// JadeEngine is a Template's config for engine type
// when use this, the templates are joker/jade
JadeEngine EngineType = 3
// AmberEngine is a Template's config for engine type
// when use this, the templates are eknkc/amber
AmberEngine EngineType = 4
2016-06-28 11:50:26 +02:00
// HandlebarsEngine is a Template's config for engine type
// when use this, the templates are aymerick/raymond
HandlebarsEngine EngineType = 5
2016-05-31 10:05:42 +02:00
// DefaultEngine is the HTMLEngine
2016-05-30 16:08:09 +02:00
DefaultEngine EngineType = HTMLEngine
2016-05-31 10:05:42 +02:00
// NoLayout to disable layout for a particular template file
2016-05-30 16:08:09 +02:00
NoLayout = "@.|.@iris_no_layout@.|.@"
2016-06-14 19:29:01 +02:00
// TemplateLayoutContextKey is the name of the user values which can be used to set a template layout from a middleware and override the parent's
TemplateLayoutContextKey = "templateLayout"
2016-05-30 16:08:09 +02:00
)
var (
// Charset character encoding.
Charset = "UTF-8"
)
type (
// Rest is a struct for specifying configuration options for the rest.Render object.
Rest struct {
// Appends the given character set to the Content-Type header. Default is "UTF-8".
Charset string
// Gzip enable it if you want to render with gzip compression. Default is false
Gzip bool
// Outputs human readable JSON.
IndentJSON bool
// Outputs human readable XML. Default is false.
IndentXML bool
// Prefixes the JSON output with the given bytes. Default is false.
PrefixJSON [ ] byte
// Prefixes the XML output with the given bytes.
PrefixXML [ ] byte
// Unescape HTML characters "&<>" to their original values. Default is false.
UnEscapeHTML bool
// Streams JSON responses instead of marshalling prior to sending. Default is false.
StreamingJSON bool
// Disables automatic rendering of http.StatusInternalServerError when an error occurs. Default is false.
DisableHTTPErrorRendering bool
// MarkdownSanitize sanitizes the markdown. Default is false.
MarkdownSanitize bool
}
2016-05-31 10:05:42 +02:00
// EngineType is the type of template engine
2016-05-30 16:08:09 +02:00
EngineType int8
2016-05-31 10:05:42 +02:00
// Template the configs for templates (template/view engines)
// contains common configs for all template engines
2016-05-30 16:08:09 +02:00
Template struct {
2016-05-31 10:05:42 +02:00
// Engine the type of template engine
// default is DefaultEngine (HTMLEngine)
2016-05-30 16:08:09 +02:00
Engine EngineType
2016-05-31 10:05:42 +02:00
// Gzip enable gzip compression
// default is false
Gzip bool
2016-05-30 16:08:09 +02:00
// Minify minifies the html result,
// Note: according to this https://github.com/tdewolff/minify/issues/35, also it removes some </tags> when minify on writer, remove this from Iris until fix.
// Default is false
//Minify bool
2016-05-31 10:05:42 +02:00
// IsDevelopment re-builds the templates on each request
// default is false
2016-05-30 16:08:09 +02:00
IsDevelopment bool
2016-05-31 10:05:42 +02:00
// Directory the system path which the templates live
// default is ./templates
Directory string
// Extensions the allowed file extension
// default is []string{".html"}
Extensions [ ] string
// ContentType is the Content-Type response header
// default is text/html but you can change if if needed
ContentType string
// Charset the charset, default is UTF-8
Charset string
// Asset is a func which returns bytes, use it to load the templates by binary
Asset func ( name string ) ( [ ] byte , error )
// AssetNames should returns the template filenames, look Asset
AssetNames func ( ) [ ] string
// Layout the template file ( with its extension) which is the mother of all
// use it to have it as a root file, and include others with {{ yield }}, refer the docs
Layout string
// HTMLTemplate contains specific configs for HTMLTemplate standard html/template
HTMLTemplate HTMLTemplate
2016-06-06 00:37:32 +02:00
// Jade contains specific configs for Jade
Jade Jade
// Pongo contains specific configs for pongo2
2016-05-31 10:05:42 +02:00
Pongo Pongo
2016-06-06 00:37:32 +02:00
// Markdown contains specific configs for markdown
2016-05-31 10:05:42 +02:00
// this doesn't supports Layout & binding context
Markdown Markdown
// Amber contains specific configs for amber
Amber Amber
2016-06-28 11:50:26 +02:00
// Handlebars contains specific configs for handlebars
Handlebars Handlebars
2016-05-30 16:08:09 +02:00
}
2016-05-31 10:05:42 +02:00
// HTMLTemplate the configs for HTMLEngine
2016-05-30 16:08:09 +02:00
HTMLTemplate struct {
2016-05-31 10:05:42 +02:00
// RequirePartials default is false
2016-05-30 16:08:09 +02:00
RequirePartials bool
// Delims
2016-05-31 10:05:42 +02:00
// Left delimeter, default is {{
Left string
// Right delimeter, default is }}
2016-05-30 16:08:09 +02:00
Right string
2016-06-02 03:45:03 +02:00
// Funcs like html/template
2016-06-02 17:27:35 +02:00
Funcs map [ string ] interface { }
2016-06-05 12:39:45 +02:00
// LayoutFuncs like html/template
2016-06-02 03:45:03 +02:00
// the difference from Funcs is that these funcs
// can be used inside a layout and can override the predefined (yield,partial...) or add more custom funcs
// these can override the Funcs inside no-layout templates also, use it when you know what you're doing
2016-06-02 17:27:35 +02:00
LayoutFuncs map [ string ] interface { }
2016-05-30 16:08:09 +02:00
}
2016-06-06 00:37:32 +02:00
// Jade the configs for JadeEngine
Jade HTMLTemplate
2016-05-31 10:05:42 +02:00
// Pongo the configs for PongoEngine
2016-05-30 16:08:09 +02:00
Pongo struct {
// Filters for pongo2, map[name of the filter] the filter function . The filters are auto register
Filters map [ string ] pongo2 . FilterFunction
// Globals share context fields between templates. https://github.com/flosch/pongo2/issues/35
Globals map [ string ] interface { }
}
2016-05-31 10:05:42 +02:00
// Markdown the configs for MarkdownEngine
2016-05-30 16:08:09 +02:00
Markdown struct {
Sanitize bool // if true then returns safe html, default is false
}
2016-05-31 10:05:42 +02:00
// Amber the configs for AmberEngine
2016-05-30 16:08:09 +02:00
Amber struct {
// Funcs for the html/template result, amber default funcs are not overrided so use it without worries
Funcs template . FuncMap
}
2016-06-28 11:50:26 +02:00
// Handlebars the configs for HandlebarsEngine
Handlebars struct {
// Helpers for Handlebars, you can register your own by raymond.RegisterHelper(name string, a interface{}) or RegisterHelpers(map[string]interface{})
// or just fill this method, do not override it it is not nil by default (because of Iris' helpers (url and urlpath)
Helpers map [ string ] interface { }
}
2016-05-30 16:08:09 +02:00
)
// DefaultRest returns the default config for rest
func DefaultRest ( ) Rest {
return Rest {
Charset : Charset ,
IndentJSON : false ,
IndentXML : false ,
PrefixJSON : [ ] byte ( "" ) ,
PrefixXML : [ ] byte ( "" ) ,
UnEscapeHTML : false ,
StreamingJSON : false ,
DisableHTTPErrorRendering : false ,
MarkdownSanitize : false ,
}
}
// Merge merges the default with the given config and returns the result
func ( c Rest ) Merge ( cfg [ ] Rest ) ( config Rest ) {
if len ( cfg ) > 0 {
config = cfg [ 0 ]
mergo . Merge ( & config , c )
} else {
_default := c
config = _default
}
return
}
// MergeSingle merges the default with the given config and returns the result
func ( c Rest ) MergeSingle ( cfg Rest ) ( config Rest ) {
config = cfg
mergo . Merge ( & config , c )
return
}
2016-05-31 10:05:42 +02:00
// DefaultTemplate returns the default template configs
2016-05-30 16:08:09 +02:00
func DefaultTemplate ( ) Template {
return Template {
Engine : DefaultEngine , //or HTMLTemplate
Gzip : false ,
IsDevelopment : false ,
Directory : "templates" ,
Extensions : [ ] string { ".html" } ,
ContentType : "text/html" ,
Charset : "UTF-8" ,
Layout : "" , // currently this is the only config which not working for pongo2 yet but I will find a way
2016-06-02 17:27:35 +02:00
HTMLTemplate : HTMLTemplate { Left : "{{" , Right : "}}" , Funcs : make ( map [ string ] interface { } , 0 ) , LayoutFuncs : make ( map [ string ] interface { } , 0 ) } ,
2016-06-06 00:37:32 +02:00
Jade : Jade { Left : "{{" , Right : "}}" , Funcs : make ( map [ string ] interface { } , 0 ) , LayoutFuncs : make ( map [ string ] interface { } , 0 ) } ,
2016-05-30 16:08:09 +02:00
Pongo : Pongo { Filters : make ( map [ string ] pongo2 . FilterFunction , 0 ) , Globals : make ( map [ string ] interface { } , 0 ) } ,
Markdown : Markdown { Sanitize : false } ,
Amber : Amber { Funcs : template . FuncMap { } } ,
2016-06-28 11:50:26 +02:00
Handlebars : Handlebars { Helpers : make ( map [ string ] interface { } , 0 ) } ,
2016-05-30 16:08:09 +02:00
}
}
// Merge merges the default with the given config and returns the result
func ( c Template ) Merge ( cfg [ ] Template ) ( config Template ) {
if len ( cfg ) > 0 {
config = cfg [ 0 ]
mergo . Merge ( & config , c )
} else {
_default := c
config = _default
}
return
}
// MergeSingle merges the default with the given config and returns the result
func ( c Template ) MergeSingle ( cfg Template ) ( config Template ) {
config = cfg
mergo . Merge ( & config , c )
return
}