template

package
v2.9.4 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TemplateAttr

type TemplateAttr func(t Templater)

func Map

func Map(s map[string]any) TemplateAttr

Map 传入 map[string]any 作为模板数据源

func Struct

func Struct(s any) TemplateAttr

Struct 传入结构体或结构体指针作为模板数据源

type Templater

type Templater interface {
	New(content string, attrs ...TemplateAttr) Templater
	Error() error
	String() string
	Bytes() []byte
	Content() string
	// contains filtered or unexported methods
}

TemplaterImpl 基于 struct tag 的模板渲染 通过 `template:"name"` 标签定义模板变量,自动将模板中的 {{name}} 替换为字段值

type TemplaterImpl added in v2.9.1

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

TemplaterImpl 基于 struct tag 的模板渲染 通过 `template:"name"` 标签定义模板变量,自动将模板中的 {{name}} 替换为字段值

func (*TemplaterImpl) Bytes added in v2.9.1

func (my *TemplaterImpl) Bytes() []byte

func (*TemplaterImpl) Content added in v2.9.1

func (my *TemplaterImpl) Content() string

func (*TemplaterImpl) Error added in v2.9.1

func (my *TemplaterImpl) Error() error

func (*TemplaterImpl) New added in v2.9.1

func (my *TemplaterImpl) New(content string, attrs ...TemplateAttr) Templater

New 根据结构体的 template tag 渲染模板 content 中使用 {{tagName}} 作为占位符,tagName 对应结构体字段上的 `template:"tagName"` 标签值

示例:

type Data struct {
    Name string `template:"name"`
    Age  int    `template:"age"`
}
tpl := "Hello {{name}}, you are {{age}} years old"
result := NewTemplateV2(tpl, Data{Name: "Tom", Age: 18}).String()
// => "Hello Tom, you are 18 years old"

func (*TemplaterImpl) String added in v2.9.1

func (my *TemplaterImpl) String() string

Jump to

Keyboard shortcuts

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