Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" gcore "github.com/snail007/gmc/core" glog "github.com/snail007/gmc/module/log" "io/ioutil" ) func main() { defaultTpl.binData = map[string][]byte{} ctx := gcore.ProviderCtx()() ctx.Logger().SetOutput(glog.NewLoggerWriter(ioutil.Discard)) ctx.SetConfig(gcore.ProviderConfig()()) tpl, err := NewTemplate(ctx, "tests/views") if err != nil { fmt.Println(err) return } tpl.Funcs(map[string]interface{}{ "add": add, }) tpl.Extension(".html") err = tpl.Parse() if err != nil { fmt.Println(err) return } b, err := tpl.Execute("user/list", map[string]string{ "head": "hello", }) if err != nil { fmt.Println(err) return } fmt.Println(string(b)) } func add(a, b string) string { return a + ">>>" + b }
Output: hello
Index ¶
- Variables
- func Init(ctx gcore.Ctx) (tpl gcore.Template, err error)
- func RenderBytes(tpl []byte, data map[string]interface{}) (result []byte, err error)
- func RenderBytesWithFunc(tpl []byte, data map[string]interface{}, funcMap map[string]interface{}) (result []byte, err error)
- func RenderString(tpl string, data map[string]interface{}) (result string, err error)
- func RenderStringWithFunc(tpl string, data map[string]interface{}, funcMap map[string]interface{}) (result string, err error)
- func SetBinBase64(data map[string]string)
- func SetBinBytes(files map[string][]byte)
- func SetBinString(files map[string]string)
- type EmbedTemplateFS
- type Render
- type Template
- func (s *Template) BinData() map[string][]byte
- func (s *Template) Ctx() gcore.Ctx
- func (s *Template) DdisableLogging()
- func (s *Template) Delims(left, right string)
- func (s *Template) DisableLoadDefaultBinData()
- func (s *Template) Execute(name string, data interface{}) (output []byte, err error)
- func (s *Template) Ext() string
- func (s *Template) Extension(ext string)
- func (s *Template) Funcs(funcMap map[string]interface{})
- func (s *Template) Parse() (err error)
- func (s *Template) RootDir() string
- func (s *Template) SetBinBase64(binData map[string]string)
- func (s *Template) SetBinBytes(binData map[string][]byte)
- func (s *Template) SetBinString(binData map[string]string)
- func (s *Template) SetCtx(ctx gcore.Ctx)
- func (s *Template) SetExt(ext string)
- func (s *Template) SetRootDir(rootDir string) error
- func (s *Template) SetTpl(tpl *gotemplate.Template)
- func (s *Template) String() string
- func (s *Template) Tpl() *gotemplate.Template
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultRender = NewRender()
Functions ¶
func RenderBytes ¶
func RenderBytesWithFunc ¶
func RenderString ¶
func RenderStringWithFunc ¶
func SetBinBase64 ¶
SetBinBase64 key is file path no slash prefix, value is file base64 encoded bytes contents.
func SetBinBytes ¶
SetBinBytes key is file path no slash prefix, value is file's bytes contents.
func SetBinString ¶
SetBinString key is file path no slash prefix, value is file's string contents.
Types ¶
type EmbedTemplateFS ¶
type EmbedTemplateFS struct {
// contains filtered or unexported fields
}
func NewEmbedTemplateFS ¶
func NewEmbedTemplateFS(tpl *Template, fs embed.FS, rootDir string) *EmbedTemplateFS
NewEmbedTemplateFS parse template files from fs embed.FS to tpl *Template, it should be called before tpl.Parse(), if the tpl parsed already , nil returned.
func (*EmbedTemplateFS) Parse ¶
func (s *EmbedTemplateFS) Parse() (err error)
func (*EmbedTemplateFS) SetExt ¶
func (s *EmbedTemplateFS) SetExt(ext string) *EmbedTemplateFS
type Render ¶
type Render struct {
// contains filtered or unexported fields
}
func (*Render) AddFuncMap ¶
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
func NewTemplate ¶
NewTemplate create a template object, and config it. rootDir is root path of view files folder.
func (*Template) DdisableLogging ¶
func (s *Template) DdisableLogging()
func (*Template) Delims ¶
Delims sets the action delimiters to the specified strings, to be used in subsequent calls to Parse. Nested template definitions will inherit the settings. An empty delimiter stands for the corresponding default: {{ or }}. The return value is the template, so calls can be chained.
func (*Template) DisableLoadDefaultBinData ¶
func (s *Template) DisableLoadDefaultBinData()
func (*Template) Execute ¶
Execute applies the view file associated with t that has the given name to the specified data object and return the output. If an error occurs executing the template,execution stops. A template may be executed safely in parallel.
func (*Template) Extension ¶
Extension sets template file extension, default is : .html only files have the extension will be parsed.
func (*Template) Funcs ¶
Funcs adds the elements of the argument map to the template's function map. It must be called before the template is parsed. It panics if a value in the map is not a function with appropriate return type or if the name cannot be used syntactically as a function in a template. It is legal to overwrite elements of the map. The return value is the template, so calls can be chained.
func (*Template) Parse ¶
Parse load all view files data and parse it to internal template object. Mutiple call of Parse() only the first call worked.
func (*Template) SetBinBase64 ¶
SetBinBase64 key is file path no slash prefix, value is file base64 encoded bytes contents.
func (*Template) SetBinBytes ¶
SetBinBytes key is file path no slash prefix, value is file's bytes contents.
func (*Template) SetBinString ¶
SetBinString key is file path no slash prefix, value is file's string contents.
func (*Template) SetRootDir ¶
func (*Template) SetTpl ¶
func (s *Template) SetTpl(tpl *gotemplate.Template)
func (*Template) Tpl ¶
func (s *Template) Tpl() *gotemplate.Template