Skip to content

Commit 4d33d3d

Browse files
committed
update
1 parent 97692af commit 4d33d3d

File tree

1 file changed

+157
-141
lines changed

1 file changed

+157
-141
lines changed

middleware/tplfunc/tplfunc.go

Lines changed: 157 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -54,171 +54,187 @@ func New() (r template.FuncMap) {
5454
return
5555
}
5656

57+
// TplFuncMap is a comprehensive template function map that provides a rich set of utility functions
58+
// for template rendering. It includes functions for time manipulation, type conversion, string processing,
59+
// encoding/decoding, mathematical operations, and more.
60+
//
61+
// TplFuncMap 是一个全面的模板函数映射,提供了丰富的实用函数集合用于模板渲染。它包括时间操作、
62+
// 类型转换、字符串处理、编码/解码、数学运算等功能。
63+
//
64+
// Function Categories / 函数分类:
65+
// - Time / 时间:Now, UnixTime, FriendlyTime, DateFormat, Ts2time, Ts2date, etc.
66+
// - Comparison / 比较:Eq, Add, Sub, Div, Mul, IsNil, IsEmpty, NotEmpty, etc.
67+
// - Type Conversion / 类型转换:Html, Js, Css, Str, Int, Float64, ToFixed, etc.
68+
// - String / 字符串:Contains, Trim, ToLower, Concat, Split, Join, etc.
69+
// - Encode & Decode / 编码解码:JSONEncode, Base64Encode, URLEncode, etc.
70+
// - Map & Slice / 映射和切片:MakeMap, MakeSlice, InSet, Append, etc.
71+
// - RegExp / 正则表达式:Regexp, RegexpPOSIX
72+
// - Other / 其他:If, Default, Hash, CaptchaForm, etc.
5773
var TplFuncMap template.FuncMap = template.FuncMap{
5874
// ======================
59-
// time
75+
// time / 时间相关
6076
// ======================
61-
"Now": Now,
62-
"UnixTime": UnixTime,
63-
"ElapsedMemory": com.ElapsedMemory, //内存消耗
64-
"TotalRunTime": com.TotalRunTime, //运行时长(从启动服务时算起)
65-
"CaptchaForm": CaptchaForm, //验证码图片
66-
"FormatByte": com.FormatBytes, //字节转为适合理解的格式
67-
"FormatBytes": com.FormatBytes, //字节转为适合理解的格式
68-
"FriendlyTime": FriendlyTime,
69-
"FormatPastTime": com.FormatPastTime, //以前距离现在多长时间
70-
"DateFormat": com.DateFormat,
71-
"DateFormatShort": com.DateFormatShort,
72-
"Ts2time": TsToTime, // 时间戳数字转time.Time
73-
"Ts2date": TsToDate, // 时间戳数字转日期字符串
77+
"Now": Now, // Current time / 当前时间
78+
"UnixTime": UnixTime, // Unix timestamp / Unix时间戳
79+
"ElapsedMemory": com.ElapsedMemory, // Memory consumption / 内存消耗
80+
"TotalRunTime": com.TotalRunTime, // Runtime since service started / 运行时长(从启动服务时算起)
81+
"CaptchaForm": CaptchaForm, // CAPTCHA image form / 验证码图片表单
82+
"FormatByte": com.FormatBytes, // Bytes to human-readable format / 字节转为适合理解的格式
83+
"FormatBytes": com.FormatBytes, // Bytes to human-readable format / 字节转为适合理解的格式
84+
"FriendlyTime": FriendlyTime, // Human-friendly time format / 友好的时间格式
85+
"FormatPastTime": com.FormatPastTime, // Time elapsed since past / 以前距离现在多长时间
86+
"DateFormat": com.DateFormat, // Date format / 日期格式化
87+
"DateFormatShort": com.DateFormatShort, // Short date format / 短日期格式
88+
"Ts2time": TsToTime, // Timestamp to time.Time / 时间戳数字转time.Time
89+
"Ts2date": TsToDate, // Timestamp to date string / 时间戳数字转日期字符串
7490

7591
// ======================
76-
// compare
92+
// compare / 比较
7793
// ======================
78-
"Eq": Eq,
79-
"Add": Add,
80-
"Sub": Sub,
81-
"Div": Div,
82-
"Mul": Mul,
83-
"IsNil": IsNil,
84-
"IsEmpty": IsEmpty,
85-
"NotEmpty": NotEmpty,
86-
"IsNaN": IsNaN,
87-
"IsInf": IsInf,
94+
"Eq": Eq, // Equal comparison / 等值比较
95+
"Add": Add, // Addition / 加法
96+
"Sub": Sub, // Subtraction / 减法
97+
"Div": Div, // Division / 除法
98+
"Mul": Mul, // Multiplication / 乘法
99+
"IsNil": IsNil, // Check if nil / 检查是否为nil
100+
"IsEmpty": IsEmpty, // Check if empty / 检查是否为空
101+
"NotEmpty": NotEmpty, // Check if not empty / 检查是否不为空
102+
"IsNaN": IsNaN, // Check if NaN / 检查是否为非数字
103+
"IsInf": IsInf, // Check if infinity / 检查是否为无穷大
88104

89105
// ======================
90-
// conversion type
106+
// conversion type / 类型转换
91107
// ======================
92-
"Html": ToHTML,
93-
"Js": ToJS,
94-
"Css": ToCSS,
95-
"ToJS": ToJS,
96-
"ToCSS": ToCSS,
97-
"ToURL": ToURL,
98-
"ToHTML": ToHTML,
99-
"ToHTMLAttr": ToHTMLAttr,
100-
"ToHTMLAttrs": ToHTMLAttrs,
101-
"ToStrSlice": ToStrSlice,
102-
"ToDuration": ToDuration,
103-
"Str": com.Str,
104-
"Int": com.Int,
105-
"Int32": com.Int32,
106-
"Int64": com.Int64,
107-
"Uint": com.Uint,
108-
"Uint32": com.Uint32,
109-
"Uint64": com.Uint64,
110-
"Float32": com.Float32,
111-
"Float64": com.Float64,
112-
"Float2int": com.Float2int,
113-
"Float2uint": com.Float2uint,
114-
"Float2int64": com.Float2int64,
115-
"Float2uint64": com.Float2uint64,
116-
"ToFloat64": ToFloat64,
117-
"ToFixed": ToFixed,
118-
"ToDecimal": ToDecimal,
119-
"NumberMore": NumberMore, // {{ 1000 | NumberMore 99 }} 99+
120-
"Math": Math,
121-
"NumberFormat": NumberFormat,
122-
"NumFormat": com.NumFormat,
123-
"NumberTrim": NumberTrim,
124-
"DurationFormat": DurationFormat,
125-
"DelimLeft": DelimLeft,
126-
"DelimRight": DelimRight,
127-
"TemplateTag": TemplateTag,
108+
"Html": ToHTML, // Convert to HTML / 转换为HTML
109+
"Js": ToJS, // Convert to JS / 转换为JavaScript
110+
"Css": ToCSS, // Convert to CSS / 转换为CSS
111+
"ToJS": ToJS, // Convert to JS / 转换为JavaScript
112+
"ToCSS": ToCSS, // Convert to CSS / 转换为CSS
113+
"ToURL": ToURL, // Convert to URL / 转换为URL
114+
"ToHTML": ToHTML, // Convert to HTML / 转换为HTML
115+
"ToHTMLAttr": ToHTMLAttr, // Convert to HTML attribute / 转换为HTML属性
116+
"ToHTMLAttrs": ToHTMLAttrs, // Convert to HTML attributes / 转换为HTML属性集合
117+
"ToStrSlice": ToStrSlice, // Convert to string slice / 转换为字符串切片
118+
"ToDuration": ToDuration, // Convert to duration / 转换为时间间隔
119+
"Str": com.Str, // Convert to string / 转换为字符串
120+
"Int": com.Int, // Convert to int / 转换为整数
121+
"Int32": com.Int32, // Convert to int32 / 转换为32位整数
122+
"Int64": com.Int64, // Convert to int64 / 转换为64位整数
123+
"Uint": com.Uint, // Convert to uint / 转换为无符号整数
124+
"Uint32": com.Uint32, // Convert to uint32 / 转换为32位无符号整数
125+
"Uint64": com.Uint64, // Convert to uint64 / 转换为64位无符号整数
126+
"Float32": com.Float32, // Convert to float32 / 转换为32位浮点数
127+
"Float64": com.Float64, // Convert to float64 / 转换为64位浮点数
128+
"Float2int": com.Float2int, // Float to int / 浮点数转整数
129+
"Float2uint": com.Float2uint, // Float to uint / 浮点数转无符号整数
130+
"Float2int64": com.Float2int64, // Float to int64 / 浮点数转64位整数
131+
"Float2uint64": com.Float2uint64, // Float to uint64 / 浮点数转64位无符号整数
132+
"ToFloat64": ToFloat64, // Convert to float64 / 转换为64位浮点数
133+
"ToFixed": ToFixed, // Format to fixed decimal / 格式化为固定位小数
134+
"ToDecimal": ToDecimal, // Convert to decimal / 转换为十进制数
135+
"NumberMore": NumberMore, // Number format with max (e.g., 99+) / 数字格式化(如:99+)
136+
"Math": Math, // Math operations / 数学运算
137+
"NumberFormat": NumberFormat, // Number formatting / 数字格式化
138+
"NumFormat": com.NumFormat, // Number formatting / 数字格式化
139+
"NumberTrim": NumberTrim, // Trim number precision / 截断数字精度
140+
"DurationFormat": DurationFormat, // Duration formatting / 时间间隔格式化
141+
"DelimLeft": DelimLeft, // Template tag left delimiter / 模板标签左分隔符
142+
"DelimRight": DelimRight, // Template tag right delimiter / 模板标签右分隔符
143+
"TemplateTag": TemplateTag, // Template tag / 模板标签
128144

129145
// ======================
130-
// string
146+
// string / 字符串处理
131147
// ======================
132-
"Contains": strings.Contains,
133-
"HasPrefix": strings.HasPrefix,
134-
"HasSuffix": strings.HasSuffix,
135-
"Trim": Trim,
136-
"TrimLeft": strings.TrimLeft,
137-
"TrimRight": strings.TrimRight,
138-
"TrimPrefix": strings.TrimPrefix,
139-
"TrimSuffix": strings.TrimSuffix,
140-
141-
"ToLower": strings.ToLower,
142-
"ToUpper": strings.ToUpper,
143-
"Title": com.Title,
144-
"LowerCaseFirst": com.LowerCaseFirst,
145-
"UpperCaseFirst": com.UpperCaseFirst,
146-
"CamelCase": com.CamelCase,
147-
"PascalCase": com.PascalCase,
148-
"SnakeCase": com.SnakeCase,
149-
"Reverse": com.Reverse,
150-
"Dir": filepath.Dir,
151-
"Base": filepath.Base,
152-
"Ext": filepath.Ext,
153-
"Dirname": path.Dir,
154-
"Basename": path.Base,
155-
"Extension": path.Ext,
156-
"InExt": InExt,
157-
158-
"Concat": Concat,
159-
"Replace": strings.Replace, //strings.Replace(s, old, new, n)
160-
"Split": strings.Split,
161-
"Join": strings.Join,
162-
"Substr": com.Substr,
163-
"StripTags": com.StripTags,
164-
"Nl2br": NlToBr, // \n替换为<br>
165-
"AddSuffix": AddSuffix,
166-
"RandomString": RandomString,
167-
"Slugify": Slugify,
168-
"SlugifyMaxWidth": SlugifyMaxWidth,
148+
"Contains": strings.Contains, // Check substring / 检查子串
149+
"HasPrefix": strings.HasPrefix, // Check prefix / 检查前缀
150+
"HasSuffix": strings.HasSuffix, // Check suffix / 检查后缀
151+
"Trim": Trim, // Trim whitespace / 去除空白
152+
"TrimLeft": strings.TrimLeft, // Trim left / 去除左侧
153+
"TrimRight": strings.TrimRight, // Trim right / 去除右侧
154+
"TrimPrefix": strings.TrimPrefix, // Trim prefix / 去除前缀
155+
"TrimSuffix": strings.TrimSuffix, // Trim suffix / 去除后缀
156+
157+
"ToLower": strings.ToLower, // To lowercase / 转小写
158+
"ToUpper": strings.ToUpper, // To uppercase / 转大写
159+
"Title": com.Title, // To title case / 标题格式
160+
"LowerCaseFirst": com.LowerCaseFirst, // Lower first letter / 首字母小写
161+
"UpperCaseFirst": com.UpperCaseFirst, // Upper first letter / 首字母大写
162+
"CamelCase": com.CamelCase, // To camel case / 转驼峰命名
163+
"PascalCase": com.PascalCase, // To pascal case / 转帕斯卡命名
164+
"SnakeCase": com.SnakeCase, // To snake case / 转蛇形命名
165+
"Reverse": com.Reverse, // Reverse string / 反转字符串
166+
"Dir": filepath.Dir, // Directory path / 目录路径
167+
"Base": filepath.Base, // Base name / 基础名称
168+
"Ext": filepath.Ext, // File extension / 文件扩展名
169+
"Dirname": path.Dir, // Directory name / 目录名
170+
"Basename": path.Base, // Base name / 基础名称
171+
"Extension": path.Ext, // Extension / 扩展名
172+
"InExt": InExt, // Check file extension / 检查文件扩展名
173+
174+
"Concat": Concat, // Concatenate strings / 连接字符串
175+
"Replace": strings.Replace, // Replace substring / 替换子串
176+
"Split": strings.Split, // Split string / 分割字符串
177+
"Join": strings.Join, // Join strings / 连接字符串
178+
"Substr": com.Substr, // Substring / 子字符串
179+
"StripTags": com.StripTags, // Strip HTML tags / 去除HTML标签
180+
"Nl2br": NlToBr, // Newline to <br> / 换行转<br>
181+
"AddSuffix": AddSuffix, // Add suffix / 添加后缀
182+
"RandomString": RandomString, // Random string / 随机字符串
183+
"Slugify": Slugify, // To slug / 转URL友好格式
184+
"SlugifyMaxWidth": SlugifyMaxWidth, // To slug with max width / 转URL友好格式(限制长度)
169185

170186
// ======================
171-
// encode & decode
187+
// encode & decode / 编码解码
172188
// ======================
173-
"JSONEncode": JSONEncode,
174-
"JSONDecode": JSONDecode,
175-
"JSONDecodeSlice": JSONDecodeSlice,
176-
"URLEncode": com.URLEncode,
177-
"URLDecode": URLDecode,
178-
"RawURLEncode": com.RawURLEncode,
179-
"RawURLDecode": URLDecode,
180-
"Base64Encode": com.Base64Encode,
181-
"Base64Decode": Base64Decode,
182-
"UnicodeDecode": UnicodeDecode,
183-
"SafeBase64Encode": com.SafeBase64Encode,
184-
"SafeBase64Decode": SafeBase64Decode,
185-
"Hash": Hash,
186-
"Unquote": Unquote,
187-
"Quote": strconv.Quote,
189+
"JSONEncode": JSONEncode, // Encode to JSON / JSON编码
190+
"JSONDecode": JSONDecode, // Decode from JSON / JSON解码
191+
"JSONDecodeSlice": JSONDecodeSlice, // Decode JSON to slice / JSON解码为切片
192+
"URLEncode": com.URLEncode, // URL encode / URL编码
193+
"URLDecode": URLDecode, // URL decode / URL解码
194+
"RawURLEncode": com.RawURLEncode, // Raw URL encode / 原始URL编码
195+
"RawURLDecode": URLDecode, // Raw URL decode / 原始URL解码
196+
"Base64Encode": com.Base64Encode, // Base64 encode / Base64编码
197+
"Base64Decode": Base64Decode, // Base64 decode / Base64解码
198+
"UnicodeDecode": UnicodeDecode, // Unicode decode / Unicode解码
199+
"SafeBase64Encode": com.SafeBase64Encode, // Safe Base64 encode / 安全Base64编码
200+
"SafeBase64Decode": SafeBase64Decode, // Safe Base64 decode / 安全Base64解码
201+
"Hash": Hash, // Hash string / 哈希字符串
202+
"Unquote": Unquote, // Unquote string / 去除引号
203+
"Quote": strconv.Quote, // Quote string / 添加引号
188204

189205
// ======================
190-
// map & slice
206+
// map & slice / 映射和切片
191207
// ======================
192-
"MakeMap": MakeMap,
193-
"MakeSlice": MakeSlice,
194-
"InSet": com.InSet,
195-
"InSlice": com.InSlice,
196-
"InSlicex": com.InSliceIface,
197-
"Set": Set,
198-
"Append": Append,
199-
"InStrSlice": InStrSlice,
200-
"SearchStrSlice": SearchStrSlice,
201-
"URLValues": URLValues,
202-
"ToSlice": ToSlice,
203-
"StrToSlice": StrToSlice,
204-
"GetByIndex": param.GetByIndex,
205-
"ToParamString": func(v string) param.String { return param.String(v) },
208+
"MakeMap": MakeMap, // Create map / 创建映射
209+
"MakeSlice": MakeSlice, // Create slice / 创建切片
210+
"InSet": com.InSet, // Check in set / 检查是否在集合中
211+
"InSlice": com.InSlice, // Check in slice / 检查是否在切片中
212+
"InSlicex": com.InSliceIface, // Check in slice (interface) / 检查是否在切片中(interface)
213+
"Set": Set, // Set value / 设置值
214+
"Append": Append, // Append to slice / 追加到切片
215+
"InStrSlice": InStrSlice, // Check in string slice / 检查是否在字符串切片中
216+
"SearchStrSlice": SearchStrSlice, // Search in string slice / 在字符串切片中搜索
217+
"URLValues": URLValues, // Create URL values / 创建URL值
218+
"ToSlice": ToSlice, // Convert to slice / 转换为切片
219+
"StrToSlice": StrToSlice, // String to slice / 字符串转切片
220+
"GetByIndex": param.GetByIndex, // Get by index / 按索引获取
221+
"ToParamString": func(v string) param.String { return param.String(v) }, // Convert to param string / 转换为参数字符串
206222

207223
// ======================
208-
// regexp
224+
// regexp / 正则表达式
209225
// ======================
210-
"Regexp": regexp.MustCompile,
211-
"RegexpPOSIX": regexp.MustCompilePOSIX,
226+
"Regexp": regexp.MustCompile, // Compile regexp / 编译正则表达式
227+
"RegexpPOSIX": regexp.MustCompilePOSIX, // Compile POSIX regexp / 编译POSIX正则表达式
212228

213229
// ======================
214-
// other
230+
// other / 其他
215231
// ======================
216-
"Ignore": Ignore,
217-
"Default": Default,
218-
"WithURLParams": com.WithURLParams,
219-
"FullURL": com.FullURL,
220-
"IsFullURL": com.IsFullURL,
221-
"If": If,
232+
"Ignore": Ignore, // Ignore value / 忽略值
233+
"Default": Default, // Default value / 默认值
234+
"WithURLParams": com.WithURLParams, // URL with params / 带参数的URL
235+
"FullURL": com.FullURL, // Full URL / 完整URL
236+
"IsFullURL": com.IsFullURL, // Check if full URL / 检查是否为完整URL
237+
"If": If, // Conditional / 条件判断
222238
}
223239

224240
var (

0 commit comments

Comments
 (0)