Documentation ¶
Overview ¶
Package gview implements a template engine based on text/template.
Reserved template variable names: I18nLanguage: Assign this variable to define i18n language for each page.
Index ¶
- Constants
- func ParseContent(ctx context.Context, content string, params ...Params) (string, error)
- type Config
- type FuncMap
- type Option
- type Params
- type View
- func (view *View) AddPath(path string) error
- func (view *View) Assign(key string, value interface{})
- func (view *View) Assigns(data Params)
- func (view *View) BindFunc(name string, function interface{})
- func (view *View) BindFuncMap(funcMap FuncMap)
- func (view *View) GetDefaultFile() string
- func (view *View) Parse(ctx context.Context, file string, params ...Params) (result string, err error)
- func (view *View) ParseContent(ctx context.Context, content string, params ...Params) (string, error)
- func (view *View) ParseDefault(ctx context.Context, params ...Params) (result string, err error)
- func (view *View) ParseOption(ctx context.Context, option Option) (result string, err error)
- func (view *View) SetAutoEncode(enable bool)
- func (view *View) SetConfig(config Config) error
- func (view *View) SetConfigWithMap(m map[string]interface{}) error
- func (view *View) SetDefaultFile(file string)
- func (view *View) SetDelimiters(left, right string)
- func (view *View) SetI18n(manager *gi18n.Manager)
- func (view *View) SetPath(path string) error
Constants ¶
const (
// DefaultName is the default group name for instance usage.
DefaultName = "default"
)
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct { Paths []string `json:"paths"` // Searching array for path, NOT concurrent-safe for performance purpose. Data map[string]interface{} `json:"data"` // Global template variables including configuration. DefaultFile string `json:"defaultFile"` // Default template file for parsing. Delimiters []string `json:"delimiters"` // Custom template delimiters. AutoEncode bool `json:"autoEncode"` // Automatically encodes and provides safe html output, which is good for avoiding XSS. I18nManager *gi18n.Manager `json:"-"` // I18n manager for the view. }
Config is the configuration object for template engine.
type FuncMap ¶
type FuncMap = map[string]interface{} // FuncMap is type for custom template functions.
type Option ¶
type Option struct { File string // Template file path in absolute or relative to searching paths. Content string // Template content, it ignores `File` if `Content` is given. Orphan bool // If true, the `File` is considered as a single file parsing without files recursively parsing from its folder. Params Params // Template parameters map. }
Option for template parsing.
type View ¶
type View struct {
// contains filtered or unexported fields
}
View object for template engine.
func Instance ¶
Instance returns an instance of View with default settings. The parameter `name` is the name for the instance.
func New ¶
New returns a new view object. The parameter `path` specifies the template directory path to load template files.
func (*View) AddPath ¶
AddPath adds an absolute or relative path to the search paths.
func (*View) Assign ¶
Assign binds a global template variable to current view object. Note that it's not concurrent-safe, which means it would panic if it's called in multiple goroutines in runtime.
func (*View) Assigns ¶
Assigns binds multiple global template variables to current view object. Note that it's not concurrent-safe, which means it would panic if it's called in multiple goroutines in runtime.
func (*View) BindFunc ¶
BindFunc registers customized global template function named `name` with given function `function` to current view object. The `name` is the function name which can be called in template content.
func (*View) BindFuncMap ¶
BindFuncMap registers customized global template functions by map to current view object. The key of map is the template function name and the value of map is the address of customized function.
func (*View) GetDefaultFile ¶
GetDefaultFile returns default template file for parsing.
func (*View) Parse ¶
func (view *View) Parse(ctx context.Context, file string, params ...Params) (result string, err error)
Parse parses given template file `file` with given template variables `params` and returns the parsed template content.
func (*View) ParseContent ¶
func (view *View) ParseContent(ctx context.Context, content string, params ...Params) (string, error)
ParseContent parses given template content `content` with template variables `params` and returns the parsed content in []byte.
func (*View) ParseDefault ¶
ParseDefault parses the default template file with params.
func (*View) ParseOption ¶
ParseOption implements template parsing using Option.
func (*View) SetAutoEncode ¶
SetAutoEncode enables/disables automatically html encoding feature. When AutoEncode feature is enables, view engine automatically encodes and provides safe html output, which is good for avoid XSS.
func (*View) SetConfig ¶
SetConfig sets the configuration for view.
func (*View) SetConfigWithMap ¶
SetConfigWithMap set configurations with map for the view.
func (*View) SetDefaultFile ¶
SetDefaultFile sets default template file for parsing.
func (*View) SetDelimiters ¶
SetDelimiters sets customized delimiters for template parsing.
func (*View) SetI18n ¶
SetI18n binds i18n manager to current view engine.