Add text and html/template `Option` func to the view engine as requested
@@ -22,6 +22,7 @@ type (
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.
// parser configuration
+ options []string // text options
left string
right string
layout string
@@ -98,6 +99,31 @@ func (s *HTMLEngine) Reload(developmentMode bool) *HTMLEngine {
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
// subsequent calls to Parse, ParseFiles, or ParseGlob. Nested template
// definitions will inherit the settings. An empty delimiter stands for the
@@ -199,7 +225,7 @@ func (s *HTMLEngine) loadDirectory() error {
name := filepath.ToSlash(rel)
tmpl := s.Templates.New(name)
-
+ tmpl.Option(s.options...)
if s.middleware != nil {
contents, err = s.middleware(name, contents)
}
@@ -263,6 +289,7 @@ func (s *HTMLEngine) loadAssets() error {
contents := string(buf)
name := filepath.ToSlash(rel)
tmpl := s.Templates.New(name)
+ tmpl.Option(s.options...)
if s.middleware != nil {
contents, err = s.middleware(name, contents)
