You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TplFuncMap is a comprehensive template function map that provides a rich set of utility functions for template rendering. It includes functions for time manipulation, type conversion, string processing, encoding/decoding, mathematical operations, and more.
快速开始 / Quick Start
import (
"html/template""github.com/webx-top/echo/middleware/tplfunc"
)
// Create template with TplFuncMap / 使用 TplFuncMap 创建模板t:=template.New("example")
t.Funcs(tplfunc.New())
// Use in template / 在模板中使用
{{ Now.Format"2006-01-02" }}
{{ ToFixed1002 }}
{{ "hello world"|ToUpper }}
函数分类 / Function Categories
1. Time Functions / 时间相关函数
函数名 Function
说明 Description
示例 Example
Now
当前时间 / Current time
{{ Now }}
UnixTime
Unix时间戳 / Unix timestamp
{{ UnixTime }}
ElapsedMemory
内存消耗 / Memory consumption
{{ ElapsedMemory }}
TotalRunTime
运行时长(从启动服务时算起) / Run time since service started
{{ TotalRunTime }}
CaptchaForm
验证码图片表单 / CAPTCHA image form
{{ CaptchaForm }}
FormatByte
字节数转为适合理解的格式 / Bytes to human-readable format
{{ 1024 | FormatByte }}
FormatBytes
字节数转为适合理解的格式 / Bytes to human-readable format
{{ 1024 | FormatBytes }}
FriendlyTime
友好的时间格式 / Human-friendly time format
{{ 3600 | FriendlyTime }}
FormatPastTime
以前距离现在多长时间 / Time elapsed since past
{{ 1609459200 | FormatPastTime }}
DateFormat
日期格式化 / Date format
{{ 1609459200 | DateFormat "2006-01-02" }}
DateFormatShort
短日期格式 / Short date format
{{ 1609459200 | DateFormatShort }}
Ts2time
时间戳数字转time.Time / Timestamp to time.Time
{{ 1609459200 | Ts2time }}
Ts2date
时间戳数字转日期字符串 / Timestamp to date string
{{ 1609459200 | Ts2date "2006-01-02" }}
2. Comparison Functions / 比较函数
函数名 Function
说明 Description
示例 Example
Eq
等值比较 / Equal comparison
{{ Eq a b }}
Add
加法 / Addition
{{ Add 1 2 }}
Sub
减法 / Subtraction
{{ Sub 5 2 }}
Div
除法 / Division
{{ Div 10 2 }}
Mul
乘法 / Multiplication
{{ Mul 3 4 }}
IsNil
检查是否为nil / Check if nil
{{ IsNil value }}
IsEmpty
检查是否为空 / Check if empty
{{ IsEmpty value }}
NotEmpty
检查是否不为空 / Check if not empty
{{ NotEmpty value }}
IsNaN
检查是否为非数字 / Check if NaN
{{ IsNaN value }}
IsInf
检查是否为无穷大 / Check if infinity
{{ IsInf value 1 }}
3. Type Conversion Functions / 类型转换函数
函数名 Function
说明 Description
示例 Example
Html, ToHTML
转换为HTML / Convert to HTML
{{ content | ToHTML }}
Js, ToJS
转换为JavaScript / Convert to JS
{{ content | ToJS }}
Css, ToCSS
转换为CSS / Convert to CSS
{{ content | ToCSS }}
ToURL
转换为URL / Convert to URL
{{ url | ToURL }}
ToHTMLAttr
转换为HTML属性 / Convert to HTML attribute
{{ attr | ToHTMLAttr }}
ToHTMLAttrs
转换为HTML属性集合 / Convert to HTML attributes
{{ attrs | ToHTMLAttrs }}
ToStrSlice
转换为字符串切片 / Convert to string slice
{{ ToStrSlice "a" "b" }}
ToDuration
转换为时间间隔 / Convert to duration
{{ ToDuration 60 "s" }}
Str
转换为字符串 / Convert to string
{{ 123 | Str }}
Int
转换为整数 / Convert to int
{{ "123" | Int }}
Int32
转换为32位整数 / Convert to int32
{{ "123" | Int32 }}
Int64
转换为64位整数 / Convert to int64
{{ "123" | Int64 }}
Uint
转换为无符号整数 / Convert to uint
{{ "123" | Uint }}
Uint32
转换为32位无符号整数 / Convert to uint32
{{ "123" | Uint32 }}
Uint64
转换为64位无符号整数 / Convert to uint64
{{ "123" | Uint64 }}
Float32
转换为32位浮点数 / Convert to float32
{{ "3.14" | Float32 }}
Float64
转换为64位浮点数 / Convert to float64
{{ "3.14" | Float64 }}
Float2int
浮点数转整数 / Float to int
{{ 3.9 | Float2int }}
Float2uint
浮点数转无符号整数 / Float to uint
{{ 3.9 | Float2uint }}
Float2int64
浮点数转64位整数 / Float to int64
{{ 3.9 | Float2int64 }}
Float2uint64
浮点数转64位无符号整数 / Float to uint64
{{ 3.9 | Float2uint64 }}
ToFloat64
转换为64位浮点数 / Convert to float64
{{ "3.14" | ToFloat64 }}
ToFixed
格式化为固定位小数 / Format to fixed decimal
{{ ToFixed 3.14159 2 }}
ToDecimal
转换为 decimal.Decimal 类型 / Converts any numeric type to a decimal.Decimal
线程安全 / Thread Safety: TplFuncMap 是全局变量,在并发环境中使用是安全的。 TplFuncMap is a global variable and is safe to use in concurrent environments.
错误处理 / Error Handling: 部分函数在遇到错误时会记录日志并返回默认值。
Some functions log errors and return default values when errors occur.
性能考虑 / Performance Considerations: 频繁调用复杂函数(如 Math)可能影响性能,建议在控制器层处理复杂逻辑。
Frequent calls to complex functions (like Math) may impact performance. Consider handling complex logic at the controller level.
HTML安全 / HTML Security: 使用 ToHTML、ToJS、ToCSS 等函数时,确保内容是可信的,以避免XSS攻击。
When using functions like ToHTML, ToJS, ToCSS, ensure the content is trusted to avoid XSS attacks.
日期格式 / Date Format: Go使用特定的日期格式参考时间 "2006-01-02 15:04:05",而不是常用的格式字符串。
Go uses a specific reference date "2006-01-02 15:04:05" for date formatting instead of common format strings.
常见问题 / FAQ
Q: 如何自定义函数 / How to add custom functions? / A: 可以通过 New() 函数创建新的 FuncMap 并添加自定义函数: