gtemplate

package
v0.0.0-...-9935139 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 11, 2024 License: MIT Imports: 19 Imported by: 2

README

GMC TEMPLATE

GMC template wrapped text/template, add more features to it.

Details about of template please read about http/views/README.md.

HELPER FUNCTIONS

GMC template helper functions is subset and based on github.com/Masterminds/sprig.

Full Documents at template/sprig/docs.

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

Examples

Constants

This section is empty.

Variables

View Source
var DefaultRender = NewRender()

Functions

func Init

func Init(ctx gcore.Ctx) (tpl gcore.Template, err error)

func RenderBytes

func RenderBytes(tpl []byte, data map[string]interface{}) (result []byte, err error)

func RenderBytesWithFunc

func RenderBytesWithFunc(tpl []byte, data map[string]interface{}, funcMap map[string]interface{}) (result []byte, err error)

func RenderString

func RenderString(tpl string, data map[string]interface{}) (result string, err error)

func RenderStringWithFunc

func RenderStringWithFunc(tpl string, data map[string]interface{}, funcMap map[string]interface{}) (result string, err error)

func SetBinBase64

func SetBinBase64(data map[string]string)

SetBinBase64 key is file path no slash prefix, value is file base64 encoded bytes contents.

func SetBinBytes

func SetBinBytes(files map[string][]byte)

SetBinBytes key is file path no slash prefix, value is file's bytes contents.

func SetBinString

func SetBinString(files map[string]string)

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 NewRender

func NewRender() *Render

func (*Render) AddFuncMap

func (s *Render) AddFuncMap(m map[string]interface{}) *Render

func (*Render) Delims

func (s *Render) Delims(left, right string) *Render

func (*Render) Parse

func (s *Render) Parse(tplBytesOrString interface{}, tplData map[string]interface{}) (d []byte, err error)

Parse the template, tplBytesOrString is []byte or string template

type Template

type Template struct {
	// contains filtered or unexported fields
}

func New

func New() (t *Template)

func NewTemplate

func NewTemplate(ctx gcore.Ctx, rootDir string) (t *Template, err error)

NewTemplate create a template object, and config it. rootDir is root path of view files folder.

func (*Template) BinData

func (s *Template) BinData() map[string][]byte

func (*Template) Ctx

func (s *Template) Ctx() gcore.Ctx

func (*Template) DdisableLogging

func (s *Template) DdisableLogging()

func (*Template) Delims

func (s *Template) Delims(left, right string)

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

func (s *Template) Execute(name string, data interface{}) (output []byte, err error)

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) Ext

func (s *Template) Ext() string

func (*Template) Extension

func (s *Template) Extension(ext string)

Extension sets template file extension, default is : .html only files have the extension will be parsed.

func (*Template) Funcs

func (s *Template) Funcs(funcMap map[string]interface{})

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

func (s *Template) Parse() (err error)

Parse load all view files data and parse it to internal template object. Mutiple call of Parse() only the first call worked.

func (*Template) RootDir

func (s *Template) RootDir() string

func (*Template) SetBinBase64

func (s *Template) SetBinBase64(binData map[string]string)

SetBinBase64 key is file path no slash prefix, value is file base64 encoded bytes contents.

func (*Template) SetBinBytes

func (s *Template) SetBinBytes(binData map[string][]byte)

SetBinBytes key is file path no slash prefix, value is file's bytes contents.

func (*Template) SetBinString

func (s *Template) SetBinString(binData map[string]string)

SetBinString key is file path no slash prefix, value is file's string contents.

func (*Template) SetCtx

func (s *Template) SetCtx(ctx gcore.Ctx)

func (*Template) SetExt

func (s *Template) SetExt(ext string)

func (*Template) SetRootDir

func (s *Template) SetRootDir(rootDir string) error

func (*Template) SetTpl

func (s *Template) SetTpl(tpl *gotemplate.Template)

func (*Template) String

func (s *Template) String() string

func (*Template) Tpl

func (s *Template) Tpl() *gotemplate.Template

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL