Add text and html/template Option func to the view engine as requested https://github.com/kataras/iris/issues/694

Former-commit-id: 4633a62addf6a25ee99d1e7f4c00f6e3555cee76
This commit is contained in:
kataras 2017-08-01 07:22:30 +03:00
parent f9c56c72e0
commit e3cf2f592a

View File

@ -22,6 +22,7 @@ type (
namesFn func() []string // for embedded, in combination with directory & extension namesFn func() []string // for embedded, in combination with directory & extension
reload bool // if true, each time the ExecuteWriter is called the templates will be reloaded. reload bool // if true, each time the ExecuteWriter is called the templates will be reloaded.
// parser configuration // parser configuration
options []string // text options
left string left string
right string right string
layout string layout string
@ -98,6 +99,31 @@ func (s *HTMLEngine) Reload(developmentMode bool) *HTMLEngine {
return s return s
} }
// Option sets options for the template. Options are described by
// strings, either a simple string or "key=value". There can be at
// most one equals sign in an option string. If the option string
// is unrecognized or otherwise invalid, Option panics.
//
// Known options:
//
// missingkey: Control the behavior during execution if a map is
// indexed with a key that is not present in the map.
// "missingkey=default" or "missingkey=invalid"
// The default behavior: Do nothing and continue execution.
// If printed, the result of the index operation is the string
// "<no value>".
// "missingkey=zero"
// The operation returns the zero value for the map type's element.
// "missingkey=error"
// Execution stops immediately with an error.
//
func (s *HTMLEngine) Option(opt ...string) *HTMLEngine {
s.mu.Lock()
s.options = append(s.options, opt...)
s.mu.Unlock()
return s
}
// Delims sets the action delimiters to the specified strings, to be used in // Delims sets the action delimiters to the specified strings, to be used in
// subsequent calls to Parse, ParseFiles, or ParseGlob. Nested template // subsequent calls to Parse, ParseFiles, or ParseGlob. Nested template
// definitions will inherit the settings. An empty delimiter stands for the // definitions will inherit the settings. An empty delimiter stands for the
@ -199,7 +225,7 @@ func (s *HTMLEngine) loadDirectory() error {
name := filepath.ToSlash(rel) name := filepath.ToSlash(rel)
tmpl := s.Templates.New(name) tmpl := s.Templates.New(name)
tmpl.Option(s.options...)
if s.middleware != nil { if s.middleware != nil {
contents, err = s.middleware(name, contents) contents, err = s.middleware(name, contents)
} }
@ -263,6 +289,7 @@ func (s *HTMLEngine) loadAssets() error {
contents := string(buf) contents := string(buf)
name := filepath.ToSlash(rel) name := filepath.ToSlash(rel)
tmpl := s.Templates.New(name) tmpl := s.Templates.New(name)
tmpl.Option(s.options...)
if s.middleware != nil { if s.middleware != nil {
contents, err = s.middleware(name, contents) contents, err = s.middleware(name, contents)