{"record":{"id":"145b092dd49d3370","repo":"siyuan-note/siyuan","slug":"execute-template-s-failed-s","errorCode":null,"errorMessage":"execute template [%s] failed: %s","messagePattern":"execute template \\[(.+?)\\] failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/av/calc_template.go","lineNumber":98,"sourceCode":"\n// evalRollupTemplate 使用 text/template + sprig 渲染自定义模板统计内容。\n// 返回渲染后的字符串；若该字符串可解析为数字则 isNumber 为 true 且 asNumber 为该数值。\n// 解析或执行失败时返回 err，由调用方决定如何提示用户。\nfunc evalRollupTemplate(templateContent string, ctx map[string]any) (rendered string, asNumber float64, isNumber bool, err error) {\n\tif \"\" == templateContent {\n\t\treturn\n\t}\n\n\tgoTpl := template.New(\"\").Delims(\".action{\", \"}\").Funcs(templateFuncMap())\n\ttpl, parseErr := goTpl.Parse(templateContent)\n\tif nil != parseErr {\n\t\terr = fmt.Errorf(\"parse template [%s] failed: %s\", templateContent, parseErr)\n\t\treturn\n\t}\n\n\tbuf := &bytes.Buffer{}\n\tif execErr := tpl.Execute(buf, ctx); nil != execErr {\n\t\terr = fmt.Errorf(\"execute template [%s] failed: %s\", templateContent, execErr)\n\t\treturn\n\t}\n\n\trendered = buf.String()\n\tif \"<no value>\" == rendered {\n\t\trendered = \"\"\n\t\treturn\n\t}\n\n\t// 渲染结果若可解析为数字，则按数字处理（前端会按列数字格式显示）\n\ttrimmed := strings.TrimSpace(rendered)\n\tif \"\" != trimmed {\n\t\tif num, parseErr := strconv.ParseFloat(trimmed, 64); nil == parseErr {\n\t\t\tasNumber = num\n\t\t\tisNumber = true\n\t\t}\n\t}\n\treturn","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/av/calc_template.go#L80-L116","documentation":"This error wraps a Go template execution failure while rendering a rollup/template column value in an attribute view. The template parsed successfully but tpl.Execute failed when evaluating it against the row context (ctx), typically because the template calls functions or fields that error at runtime. The error message includes the full template content and the underlying exec error.","triggerScenarios":"Calling calcFieldRollup or calcFieldByTemplate when a template column's content parses but fails during execution against a row's ctx, e.g. referencing unsupported sprig/templ functions, calling functions with wrong argument types, or dereferencing missing fields via index/template actions that panic-return errors.","commonSituations":"Users paste templates copied from other tools with unsupported functions; a template written for a different context shape (e.g. uses .Name when the row context has no such key); a rollup referencing a template that performs arithmetic on non-numeric cell values.","solutions":["Read the wrapped exec error in the message to identify the failing template action or function","Simplify the template to isolate the failing action; test with a minimal template first","Verify all functions used are supported by the template func map exposed by the kernel (lute/sprig subset)","Check that referenced column names/values in the row context exist and have compatible types"],"exampleFix":"// before\ncalcValue = {{ div .Stock 0 }}\n// after\ncalcValue = {{ if gt .Stock 0 }}{{ div .Stock 10 }}{{ else }}0{{ end }}","handlingStrategy":"try-catch","validationCode":"tpl, err := template.New(\"t\").Funcs(funcs).Parse(templateContent)\nif err != nil { return err }\nvar buf bytes.Buffer\nif err := tpl.Execute(&buf, ctx); err != nil { return err }","typeGuard":null,"tryCatchPattern":"if err := calcByTemplate(...); err != nil {\n    var tplErr *template.ExecError\n    if errors.As(err, &tplErr) { /* inspect failing line/func */ }\n}","preventionTips":["Test templates with a representative row context before saving them to the column","Only use functions from the kernel's template func map","Guard arithmetic against zero/missing values with if-actions"],"tags":["go","template","attribute-view"],"backgroundTag":"invalid-argument-format","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}