Escape input to template hash
This commit is contained in:
parent
0acc608e47
commit
07adf2d49e
@ -2,6 +2,7 @@ package templatenest
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -25,12 +26,13 @@ type Option struct {
|
|||||||
TokenEscapeChar string // Escapes a token delimiter, i.e. if set to '\' then variables that have '\' prefix won't be replaced
|
TokenEscapeChar string // Escapes a token delimiter, i.e. if set to '\' then variables that have '\' prefix won't be replaced
|
||||||
DefaultsNamespaceChar string
|
DefaultsNamespaceChar string
|
||||||
Defaults Hash // Provide a hash of default values that are substituted if template hash does not provide a value
|
Defaults Hash // Provide a hash of default values that are substituted if template hash does not provide a value
|
||||||
defaultsFlat Hash
|
NoEscapeInput bool // By default all template values are html escaped
|
||||||
}
|
}
|
||||||
|
|
||||||
type TemplateNest struct {
|
type TemplateNest struct {
|
||||||
option Option
|
option Option
|
||||||
cache map[string]TemplateFileIndex
|
defaultsFlat Hash
|
||||||
|
cache map[string]TemplateFileIndex
|
||||||
}
|
}
|
||||||
|
|
||||||
// TemplateFileIndex represents an indexed template file.
|
// TemplateFileIndex represents an indexed template file.
|
||||||
@ -83,12 +85,11 @@ func New(opts Option) (*TemplateNest, error) {
|
|||||||
opts.Defaults = make(map[string]interface{})
|
opts.Defaults = make(map[string]interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
opts.defaultsFlat = FlattenMap(opts.Defaults, "", opts.DefaultsNamespaceChar)
|
|
||||||
|
|
||||||
// Initialize TemplateNest with the final options.
|
// Initialize TemplateNest with the final options.
|
||||||
nest := &TemplateNest{
|
nest := &TemplateNest{
|
||||||
option: opts,
|
option: opts,
|
||||||
cache: make(map[string]TemplateFileIndex),
|
cache: make(map[string]TemplateFileIndex),
|
||||||
|
defaultsFlat: FlattenMap(opts.Defaults, "", opts.DefaultsNamespaceChar),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Walk through the template directory and index the templates.
|
// Walk through the template directory and index the templates.
|
||||||
@ -263,7 +264,10 @@ func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
|
|||||||
return fmt.Sprintf("%t", v), nil
|
return fmt.Sprintf("%t", v), nil
|
||||||
|
|
||||||
case string:
|
case string:
|
||||||
return v, nil
|
if nest.option.NoEscapeInput {
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
return html.EscapeString(v), nil
|
||||||
|
|
||||||
case float64, int, int64:
|
case float64, int, int64:
|
||||||
return fmt.Sprintf("%v", v), nil
|
return fmt.Sprintf("%v", v), nil
|
||||||
@ -337,7 +341,7 @@ func (nest *TemplateNest) Render(toRender interface{}) (string, error) {
|
|||||||
replacement := ""
|
replacement := ""
|
||||||
|
|
||||||
value, exists := v[variable.Name]
|
value, exists := v[variable.Name]
|
||||||
defaultValue, defaultExists := nest.option.defaultsFlat[variable.Name]
|
defaultValue, defaultExists := nest.defaultsFlat[variable.Name]
|
||||||
|
|
||||||
if exists || defaultExists {
|
if exists || defaultExists {
|
||||||
if !exists {
|
if !exists {
|
||||||
|
Loading…
Reference in New Issue
Block a user