template

package
v0.0.0-...-ff5ac03 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CompCol       = "col"       // 列组件
	CompRow       = "row"       // 行组件
	CompForm      = "form"      // 表单组件
	CompTable     = "table"     // 表格组件
	CompDataTable = "datatable" // 数据表格组件
	CompTree      = "tree"      // 树组件
	CompTreeView  = "treeview"  // 树视图组件
	CompTabs      = "tabs"      // 标签页组件
	CompAlert     = "alert"     // 警告组件
	CompLink      = "link"      // 链接组件
	CompPaginator = "paginator" // 分页组件
	CompPopup     = "popup"     // 弹窗组件
	CompBox       = "box"       // 盒子组件
	CompLabel     = "label"     // 标签组件
	CompImage     = "image"     // 图片组件
	CompButton    = "button"    // 按钮组件
)

组件类型常量定义

Variables

View Source
var DefaultFuncMap = template.FuncMap{
	"lang":     language.Get,
	"langHtml": language.GetFromHtml,
	"link": func(cdnUrl, prefixUrl, assetsUrl string) string {

		if cdnUrl == "" {
			return prefixUrl + assetsUrl
		}
		return cdnUrl + assetsUrl
	},
	"isLinkUrl": func(s string) bool {

		return (len(s) > 7 && s[:7] == "http://") || (len(s) > 8 && s[:8] == "https://")
	},
	"render": func(s, old, repl template.HTML) template.HTML {

		return template.HTML(strings.ReplaceAll(string(s), string(old), string(repl)))
	},
	"renderJS": func(s template.JS, old, repl template.HTML) template.JS {

		return template.JS(strings.ReplaceAll(string(s), string(old), string(repl)))
	},
	"divide": func(a, b int) int {

		return a / b
	},
	"renderRowDataHTML": func(id, content template.HTML, value ...map[string]types.InfoItem) template.HTML {

		return template.HTML(types.ParseTableDataTmplWithID(id, string(content), value...))
	},
	"renderRowDataJS": func(id template.HTML, content template.JS, value ...map[string]types.InfoItem) template.JS {

		return template.JS(types.ParseTableDataTmplWithID(id, string(content), value...))
	},
	"attr": func(s template.HTML) template.HTMLAttr {

		return template.HTMLAttr(s)
	},
	"js": func(s interface{}) template.JS {

		if ss, ok := s.(string); ok {
			return template.JS(ss)
		}
		if ss, ok := s.(template.HTML); ok {
			return template.JS(ss)
		}
		return ""
	},
	"changeValue": func(f types.FormField, index int) types.FormField {

		if len(f.ValueArr) > 0 {
			f.Value = template.HTML(f.ValueArr[index])
		}
		if len(f.OptionsArr) > 0 {
			f.Options = f.OptionsArr[index]
		}
		if f.FormType.IsSelect() {
			f.FieldClass += "_" + strconv.Itoa(index)
		}
		return f
	},
}

DefaultFuncMap 默认模板函数映射

View Source
var DefaultThemeNames = []string{"sword", "adminlte"}

DefaultThemeNames 默认主题名称列表

Functions

func Add

func Add(name string, temp Template)

Add 通过提供的主题名称使模板可用 如果使用相同的名称调用两次Add或模板为nil,会触发panic 参数:

  • name: 主题名称
  • temp: 模板接口

func AddComp

func AddComp(comp Component)

AddComp 通过提供的名称使组件可用 如果使用相同的名称调用两次Add或组件为nil,会触发panic 参数:

  • comp: 组件接口

func AddFromPlugin

func AddFromPlugin(name string, mod string)

AddFromPlugin 从插件添加模板 参数:

  • name: 模板名称
  • mod: 插件模块路径

func AddLoginComp

func AddLoginComp(comp Component)

AddLoginComp 添加指定的登录组件 参数:

  • comp: 组件接口

func CSS

func CSS(s string) template.CSS

CSS 将字符串转换为CSS类型 参数:

  • s: CSS字符串

返回: CSS类型

func CheckRequirements

func CheckRequirements() (bool, bool)

CheckRequirements 检查主题和GoAdmin的相互依赖限制 第一个返回参数表示GoAdmin版本是否满足所使用主题的要求 第二个返回参数表示所使用主题的版本是否满足GoAdmin的要求 返回:

  • bool: GoAdmin版本是否满足主题要求
  • bool: 主题版本是否满足GoAdmin要求

func CheckThemeRequirements

func CheckThemeRequirements() bool

CheckThemeRequirements 检查主题要求 返回: GoAdmin版本是否满足主题要求

func Execute

func Execute(ctx *context.Context, param *ExecuteParam) *bytes.Buffer

Execute 执行模板渲染 参数:

  • ctx: 上下文对象
  • param: 执行参数

返回: 渲染后的缓冲区

func GetAsset

func GetAsset(path string) ([]byte, error)

GetAsset 根据路径获取资源内容 参数:

  • path: 资源路径

返回: 资源内容和错误

func GetComponentAsset

func GetComponentAsset() []string

GetComponentAsset 获取所有组件的资源列表 返回: 资源URL后缀列表

func GetComponentAssetImportHTML

func GetComponentAssetImportHTML(ctx *context.Context) (res template.HTML)

GetComponentAssetImportHTML 获取组件资源导入HTML 参数:

  • ctx: 上下文对象

返回: 资源导入HTML

func GetComponentAssetWithinPage

func GetComponentAssetWithinPage() []string

GetComponentAssetWithinPage 获取页面内组件的资源列表(不包括页面组件) 返回: 资源URL后缀列表

func GetPageContentFromPageType

func GetPageContentFromPageType(ctx *context.Context, title, desc, msg string, pt PageType) (template.HTML, template.HTML, template.HTML)

GetPageContentFromPageType 根据页面类型获取页面内容 参数:

  • ctx: 上下文对象
  • title: 页面标题
  • desc: 页面描述
  • msg: 消息内容
  • pt: 页面类型

返回: 标题HTML、描述HTML、内容HTML

func HTML

func HTML(s string) template.HTML

HTML 将字符串转换为HTML类型 参数:

  • s: HTML字符串

返回: HTML类型

func JS

func JS(s string) template.JS

JS 将字符串转换为JS类型 参数:

  • s: JavaScript字符串

返回: JS类型

func SetComp

func SetComp(name string, comp Component)

SetComp 通过提供的名称使组件可用 如果键对应的值为空或组件为nil,会触发panic 参数:

  • name: 组件名称
  • comp: 组件接口

func Themes

func Themes() []string

Themes 获取所有已注册的主题名称 返回: 主题名称列表

func VersionCompare

func VersionCompare(toCompare string, versions []string) bool

VersionCompare 比较版本号 参数:

  • toCompare: 要比较的版本号
  • versions: 版本号列表

返回: 是否匹配或满足版本要求

func WarningPanel

func WarningPanel(ctx *context.Context, msg string, pts ...PageType) types.Panel

WarningPanel 创建警告面板 参数:

  • ctx: 上下文对象
  • msg: 消息内容
  • pts: 可选的页面类型

返回: 面板对象

func WarningPanelWithDescAndTitle

func WarningPanelWithDescAndTitle(ctx *context.Context, msg, desc, title string, pts ...PageType) types.Panel

WarningPanelWithDescAndTitle 创建带描述和标题的警告面板 参数:

  • ctx: 上下文对象
  • msg: 消息内容
  • desc: 描述
  • title: 标题
  • pts: 可选的页面类型

返回: 面板对象

Types

type BaseComponent

type BaseComponent struct {
	Name      string          // 组件名称
	HTMLData  string          // HTML数据
	CSS       template.CSS    // CSS样式
	JS        template.JS     // JavaScript代码
	Callbacks types.Callbacks // 回调函数
}

BaseComponent 基础组件结构体

func (*BaseComponent) BindActionTo

func (b *BaseComponent) BindActionTo(ctx *context.Context, action types.Action, id string)

BindActionTo 将动作绑定到组件 参数:

  • ctx: 上下文对象
  • action: 动作对象
  • id: 按钮ID

func (*BaseComponent) GetAsset

func (b *BaseComponent) GetAsset(name string) ([]byte, error)

GetAsset 根据名称获取资源 参数:

  • name: 资源名称

返回: 资源内容和错误

func (*BaseComponent) GetAssetList

func (b *BaseComponent) GetAssetList() []string

GetAssetList 获取资源列表 返回: 资源URL后缀列表

func (*BaseComponent) GetCSS

func (b *BaseComponent) GetCSS() template.CSS

GetCSS 获取CSS样式 返回: CSS样式

func (*BaseComponent) GetCallbacks

func (b *BaseComponent) GetCallbacks() types.Callbacks

GetCallbacks 获取回调函数 返回: 回调函数列表

func (*BaseComponent) GetContentWithData

func (b *BaseComponent) GetContentWithData(obj interface{}) template.HTML

GetContentWithData 使用数据获取内容 参数:

  • obj: 数据对象

返回: HTML内容

func (*BaseComponent) GetJS

func (b *BaseComponent) GetJS() template.JS

GetJS 获取JavaScript代码 返回: JavaScript代码

func (*BaseComponent) GetName

func (b *BaseComponent) GetName() string

GetName 获取组件名称 返回: 组件名称

func (*BaseComponent) GetTemplate

func (b *BaseComponent) GetTemplate() (*template.Template, string)

GetTemplate 获取模板对象 返回: 模板对象和模板名称

func (*BaseComponent) IsAPage

func (b *BaseComponent) IsAPage() bool

IsAPage 判断是否为页面组件 返回: 是否为页面组件

type Component

type Component interface {
	// GetTemplate 返回一个 *template.Template 和一个给定的键
	GetTemplate() (*template.Template, string)

	// GetAssetList 返回组件中使用的资源URL后缀
	// 示例:
	//
	// {{.UrlPrefix}}/assets/login/css/bootstrap.min.css => login/css/bootstrap.min.css
	//
	// 参见:
	// https://github.com/purpose168/GoAdmin/blob/master/template/login/theme1.tmpl#L32
	// https://github.com/purpose168/GoAdmin/blob/master/template/login/list.go
	GetAssetList() []string

	// GetAsset 根据相应的URL后缀返回资源内容
	// 建议使用go-bindata工具生成资源内容
	//
	// 参见: http://github.com/jteeuwen/go-bindata
	GetAsset(string) ([]byte, error)

	GetContent() template.HTML

	IsAPage() bool

	GetName() string

	GetJS() template.JS
	GetCSS() template.CSS
	GetCallbacks() types.Callbacks
}

Component 是表示UI组件的接口

func GetComp

func GetComp(name string) Component

GetComp 根据注册名称获取组件 如果找不到名称,会触发panic 参数:

  • name: 组件名称

返回: 组件接口

type ExecuteOptions

type ExecuteOptions struct {
	Animation         bool                           // 是否启用动画
	NoCompress        bool                           // 是否不压缩
	HideSideBar       bool                           // 是否隐藏侧边栏
	HideHeader        bool                           // 是否隐藏头部
	UpdateMenu        bool                           // 是否更新菜单
	NavDropDownButton []*types.NavDropDownItemButton // 导航下拉按钮
}

ExecuteOptions 执行选项结构体

func GetExecuteOptions

func GetExecuteOptions(options []ExecuteOptions) ExecuteOptions

GetExecuteOptions 获取执行选项 参数:

  • options: 执行选项列表

返回: 执行选项

type ExecuteParam

type ExecuteParam struct {
	User       models.UserModel   // 用户模型
	Tmpl       *template.Template // 模板对象
	TmplName   string             // 模板名称
	IsPjax     bool               // 是否为PJAX请求
	Panel      types.Panel        // 面板
	Config     *c.Config          // 配置对象
	Menu       *menu.Menu         // 菜单
	Animation  bool               // 是否启用动画
	Buttons    types.Buttons      // 按钮
	NoCompress bool               // 是否不压缩
	Iframe     bool               // 是否在iframe中
}

ExecuteParam 执行参数结构体

type PageType

type PageType uint8

PageType 页面类型枚举

const (
	NormalPage          PageType = iota // 正常页面
	Missing404Page                      // 404页面
	Error500Page                        // 500错误页面
	NoPermission403Page                 // 403无权限页面
)

func GetPageTypeFromPageError

func GetPageTypeFromPageError(err errors2.PageError) PageType

GetPageTypeFromPageError 根据页面错误获取页面类型 参数:

  • err: 页面错误对象

返回: 对应的页面类型

type Template

type Template interface {
	Name() string

	// 布局
	Col() types.ColAttribute
	Row() types.RowAttribute

	// 表单和表格
	Form() types.FormAttribute
	Table() types.TableAttribute
	DataTable() types.DataTableAttribute

	TreeView() types.TreeViewAttribute
	Tree() types.TreeAttribute
	Tabs() types.TabsAttribute
	Alert() types.AlertAttribute
	Link() types.LinkAttribute

	Paginator() types.PaginatorAttribute
	Popup() types.PopupAttribute
	Box() types.BoxAttribute

	Label() types.LabelAttribute
	Image() types.ImgAttribute

	Button() types.ButtonAttribute

	// 构建器方法
	GetTmplList() map[string]string
	GetAssetList() []string
	GetAssetImportHTML(exceptComponents ...string) template.HTML
	GetAsset(string) ([]byte, error)
	GetTemplate(bool) (*template.Template, string)
	GetVersion() string
	GetRequirements() []string
	GetHeadHTML() template.HTML
	GetFootJS() template.HTML
	Get404HTML() template.HTML
	Get500HTML() template.HTML
	Get403HTML() template.HTML
}

Template 是包含UI组件方法的接口 它将在插件中用于自定义UI

func Default

func Default(ctx ...*context.Context) Template

Default 获取使用全局配置设置的主题名称的默认模板 如果找不到名称,会触发panic 参数:

  • ctx: 可选的上下文对象

返回: 模板接口

func Get

func Get(ctx *context.Context, theme string) Template

Get 根据主题名称获取模板接口 如果找不到名称,会触发panic 参数:

  • ctx: 上下文对象
  • theme: 主题名称

返回: 模板接口

Directories

Path Synopsis
Code generated by go-bindata.
Code generated by go-bindata.
Code generated by go-bindata.
Code generated by go-bindata.
Code generated by go-bindata.
Code generated by go-bindata.

Jump to

Keyboard shortcuts

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