template

package
v0.0.0-...-dab2878 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2019 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FuncMap

type FuncMap template.FuncMap

type Kind

type Kind uint8

* 模板风格

const (
	INVALID Kind = iota // 无效模板
	TEXT                // 文本模板, 使用 "text/template" 进行处理.
	HTML                // html模板, 使用 "html/template" 进行处理.
)

type Template

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

* Template.

func New

func New(uri string, funcMap ...FuncMap) (*Template, error)

* New 基于资源路径 uri 新建一个 Template. 先对 uri 进行绝对路径计算, 计算出 rootdir 和是否要加载文件.

参数:

uri 资源路径可以是目录或者文件, 无扩展名当作目录, 否则当作文件.
	如果 uri 为空, 用 os.Getwd() 获取目录.
	如果 uri 以 `./` 或 `.\` 开头自动加当前路径, 否则当作绝对路径.
	如果 uri 含扩展当作模板文件, 使用 ParseFiles 解析.
	uri 所指的目录被设置为 rootdir, 后续载入的文件被限制在此目录下.

funcMap 可选自定义 FuncMap.
	当 uri 为文件时, funcMap 参数可保障正确解析模板中的函数.

返回:

模板实例和发生的错误.

func (*Template) AddParseTree

func (t *Template) AddParseTree(
	kind Kind, tree *parse.Tree) (*Template, error)

* AddParseTree 添加 tree. 参数:

kind 值为TEXT 或 HTML, 指示 tree 采用何种风格执行.
tree 是已经处理好的, 且 tree.Name 对应绝对路径的模板名称.

返回:

如果 tree 符合要求, 返回 tree 对应的 *Template.
否则返回 nil 和错误.

细节:

事实上 ParseFiles, ParseGlob 都调用了 AddParseTree.
如果 t 没有对应的执行模板, 自动绑定第一个 Tree 对应的模板.

func (*Template) Copy

func (t *Template) Copy() (*Template, error)

* Copy 返回一份 *Template 的拷贝. 这是真正的拷贝. 非并发安全, 如果需要 Copy 功能, 应保留一份母本专用于 Copy. 提示:

Copy 会重建 FuncMap 中的 "import" 函数.

func (*Template) Delims

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

* Delims 设置模板定界符. 返回 t.

func (*Template) Dir

func (t *Template) Dir() string

* Dir 返回 t 所在目录绝对路径. slash 分割. 尾部没有 slash.

func (*Template) Execute

func (t *Template) Execute(
	wr io.Writer, data interface{}) error

* Execute 执行模板, 把结果写入 wr.

func (*Template) ExecuteTemplate

func (t *Template) ExecuteTemplate(
	wr io.Writer, name string, data interface{}) error

* ExecuteTemplate 执行 name 对应的模板, 把结果写入 wr. 此方法先调用 Lookup 获取 name 对应的模板, 然后执行它.

func (*Template) Funcs

func (t *Template) Funcs(funcMap FuncMap) *Template

* Funcs 给模板绑定自定义 FuncMap. 参数:

funcMap 设定一次, 在所有相关模板中都会生效.

返回: t

func (*Template) Lookup

func (t *Template) Lookup(name string) *Template

* Lookup 取出 name 对应的 *Template. 参数:

name 模板名, 相对路径. 如果以 "/" 开头表示从 rootdir 开始,
否则从 t.Dir() 所在目录开始.

返回:

返回 name 对应模板, 如果 name 为空或者未找到对应模板, 返回 nil.

func (*Template) Name

func (t *Template) Name() string

* Name 返回 uri 风格的模板名, 事实是模板对应的绝对路径. 如果为空表示模板无效.

func (*Template) Parse

func (t *Template) Parse(name, text string) (*Template, error)

* Parse 解析模板源代码 text, 并以 name 命名解析后的模板. 参数:

name 模板名字, 相对于 rootdir 的绝对路径名.
text 待解析的模板源代码.

返回:

解析后的模板和发生的错误.

func (*Template) ParseFiles

func (t *Template) ParseFiles(
	filename ...string) error

* ParseFiles 解析多个模板文件. 自动跳过重复的文件. 参数:

filename 模板文件, 可使用相对路径或绝对路径.

返回:

是否有错误发生.

func (*Template) ParseGlob

func (t *Template) ParseGlob(pattern string) error

* ParseGlob 解析多个模板文件. 自动跳过重复的文件. 参数:

pattern 模板文件模式匹配.

返回:

是否有错误发生.

func (*Template) RootDir

func (t *Template) RootDir() string

* RootDir 返回 rootdir.

func (*Template) Walk

func (t *Template) Walk(dir string, exts string) error

* Walk 遍历 dir, 根据允许的扩展名加载模板文件. 要求所有加载文件必须位于 rootdir 之下. 自动跳过重复的文件. 参数:

dir  要遍历的目录.
exts 允许的扩展名拼接字符串, 格式实例: ".html.tmpl".

Jump to

Keyboard shortcuts

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