From ec7c72d68c94301f27962378eecf8c19cecbcfe5 Mon Sep 17 00:00:00 2001 From: 710leo <710leo@gmail.com> Date: Tue, 11 Mar 2025 00:15:37 +0800 Subject: [PATCH] add jsonMarshal tpl func --- pkg/tplx/fns.go | 9 +++++++++ pkg/tplx/tplx.go | 1 + 2 files changed, 10 insertions(+) diff --git a/pkg/tplx/fns.go b/pkg/tplx/fns.go index 7e1903db..dacdfea2 100644 --- a/pkg/tplx/fns.go +++ b/pkg/tplx/fns.go @@ -1,6 +1,7 @@ package tplx import ( + "encoding/json" "errors" "fmt" "html/template" @@ -653,3 +654,11 @@ func BatchContactsAtsInFeishuEmail(arr interface{}) template.HTML { func BatchContactsAtsInFeishuId(arr interface{}) template.HTML { return template.HTML(MappingAndJoin(arr, "", " ")) } + +func JsonMarshal(v interface{}) template.HTML { + json, err := json.Marshal(v) + if err != nil { + return template.HTML("") + } + return template.HTML(string(json)) +} diff --git a/pkg/tplx/tplx.go b/pkg/tplx/tplx.go index 3a1f51ea..e56b4762 100644 --- a/pkg/tplx/tplx.go +++ b/pkg/tplx/tplx.go @@ -61,6 +61,7 @@ var TemplateFuncMap = template.FuncMap{ "mappingAndJoin": MappingAndJoin, "batchContactsAtsInFeishuEmail": BatchContactsAtsInFeishuEmail, "batchContactsAtsInFeishuId": BatchContactsAtsInFeishuId, + "jsonMarshal": JsonMarshal, } // NewTemplateFuncMap copy on write for TemplateFuncMap