模板类

package
v0.0.0-...-2910145 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 30 Imported by: 0

Documentation

Overview

Package gview 实现了一个基于 text/template 的模板引擎。

预留的模板变量名称: I18nLanguage: 将此变量赋值以在每一页上定义 i18n 语言。 这段 Go 代码注释翻译成中文后为: ```go 包 gview 实现了一个基于 text/template 标准库的模板引擎功能。

已保留的模板变量名称: I18nLanguage:将该变量进行赋值,以便在每个页面上定义国际化的(i18n)语言设置。

Index

Constants

View Source
const (
	// DefaultName 是用于实例使用的默认组名称。
	DefaultName = "default"
)

Variables

This section is empty.

Functions

func ParseContent

func ParseContent(ctx context.Context, content string, params ...Params) (string, error)

ParseContent 使用默认视图对象直接解析模板内容, 并返回已解析的内容。

Types

type Config

type Config struct {
	Paths       []string               `json:"paths"`       // 为了性能考虑,以下代码在数组中搜索路径,但并不保证并发安全。
	Data        map[string]interface{} `json:"data"`        // 全局模板变量,包括配置信息。
	DefaultFile string                 `json:"defaultFile"` // 默认用于解析的模板文件。
	Delimiters  []string               `json:"delimiters"`  // 自定义模板分隔符。
	AutoEncode  bool                   `json:"autoEncode"`  // 自动进行编码并提供安全的HTML输出,有助于避免XSS攻击。
	I18nManager *gi18n.Manager         `json:"-"`           // 视图的国际化管理器。
}

Config 是用于模板引擎的配置对象。

func DefaultConfig

func DefaultConfig() Config

DefaultConfig 创建并返回一个包含默认配置的配置对象。

type FuncMap

type FuncMap = map[string]interface{} // FuncMap 是用于自定义模板函数的类型。

type Option

type Option struct {
	File    string // 模板文件路径,可以是绝对路径,也可以相对于搜索路径。
	Content string // 模板内容,如果提供了`Content`,则会忽略`File`。
	Orphan  bool   // 如果为true,那么`File`被视为单个文件解析,不递归地从其所在文件夹中解析其他文件。
	Params  Params // 模板参数映射。
}

模板解析的选项。

type Params

type Params = map[string]interface{} // Params 是模板参数的类型。

type View

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

模板引擎的视图对象。

func Instance

func Instance(name ...string) *View

Instance 返回一个使用默认设置的 View 实例。 参数 `name` 是该实例的名称。

func New

func New(path ...string) *View

New返回一个新的视图对象。 参数`path`用于指定加载模板文件的模板目录路径。

func (*View) AddPath

func (view *View) AddPath(path string) error

AddPath 将一个绝对路径或相对路径添加到搜索路径中。

func (*View) Assign

func (view *View) Assign(key string, value interface{})

Assign 将全局模板变量绑定到当前视图对象。 注意,它不是并发安全的,这意味着如果在运行时多个goroutine中调用它,将会导致panic。

func (*View) Assigns

func (view *View) Assigns(data Params)

Assigns 将多个全局模板变量绑定到当前视图对象。 注意,它不是并发安全的,这意味着如果在运行时多个goroutine中调用它,将会引发panic。

func (*View) BindFunc

func (view *View) BindFunc(name string, function interface{})

BindFunc 注册一个名为 `name` 的自定义全局模板函数到当前视图对象, 使用给定的 `function` 函数。在模板内容中,`name` 是可以被调用的函数名。

func (*View) BindFuncMap

func (view *View) BindFuncMap(funcMap FuncMap)

BindFuncMap 通过映射注册自定义的全局模板函数到当前视图对象。 映射的键是模板函数名称 映射的值是自定义函数的地址。

func (*View) GetDefaultFile

func (view *View) GetDefaultFile() string

GetDefaultFile 返回用于解析的默认模板文件。

func (*View) Parse

func (view *View) Parse(ctx context.Context, file string, params ...Params) (result string, err error)

Parse函数用于解析给定的模板文件`file`,并使用给定的模板变量`params`进行解析, 然后返回解析后的模板内容。

func (*View) ParseContent

func (view *View) ParseContent(ctx context.Context, content string, params ...Params) (string, error)

ParseContent 函数用于解析给定的模板内容 `content`,同时使用模板变量 `params` 进行替换, 并将解析后的内容以 []byte 类型返回。

func (*View) ParseDefault

func (view *View) ParseDefault(ctx context.Context, params ...Params) (result string, err error)

ParseDefault 通过给定的参数解析默认模板文件。

func (*View) ParseOption

func (view *View) ParseOption(ctx context.Context, option Option) (result string, err error)

ParseOption 实现了通过 Option 进行模板解析的功能。

func (*View) SetAutoEncode

func (view *View) SetAutoEncode(enable bool)

SetAutoEncode 用于开启或关闭自动HTML编码功能。 当自动编码功能开启时,视图引擎会自动进行编码并提供安全的HTML输出, 这有助于避免XSS(跨站脚本攻击)漏洞。

func (*View) SetConfig

func (view *View) SetConfig(config Config) error

SetConfig 设置视图的配置。

func (*View) SetConfigWithMap

func (view *View) SetConfigWithMap(m map[string]interface{}) error

SetConfigWithMap 使用map设置视图的相关配置。

func (*View) SetDefaultFile

func (view *View) SetDefaultFile(file string)

SetDefaultFile 设置用于解析的默认模板文件。

func (*View) SetDelimiters

func (view *View) SetDelimiters(left, right string)

SetDelimiters 设置用于模板解析的自定义分隔符。

func (*View) SetI18n

func (view *View) SetI18n(manager *gi18n.Manager)

SetI18n 将 i18n 管理器绑定到当前视图引擎。

func (*View) SetPath

func (view *View) SetPath(path string) error

SetPath 设置模板文件搜索的目录路径。 参数 `path` 可以是绝对路径或相对路径,但建议使用绝对路径。

Jump to

Keyboard shortcuts

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