siyuan-note/siyuan · error

execute template [ ] failed

Error message

execute template [%s] failed: %s

What it means

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.

Solutions

  1. Read the wrapped exec error in the message to identify the failing template action or function
  2. Simplify the template to isolate the failing action; test with a minimal template first
  3. Verify all functions used are supported by the template func map exposed by the kernel (lute/sprig subset)
  4. Check that referenced column names/values in the row context exist and have compatible types

Example fix

// before
calcValue = {{ div .Stock 0 }}
// after
calcValue = {{ if gt .Stock 0 }}{{ div .Stock 10 }}{{ else }}0{{ end }}
Defensive patterns

Strategy: try-catch

Validate before calling

tpl, err := template.New("t").Funcs(funcs).Parse(templateContent)
if err != nil { return err }
var buf bytes.Buffer
if err := tpl.Execute(&buf, ctx); err != nil { return err }

Try / catch

if err := calcByTemplate(...); err != nil {
    var tplErr *template.ExecError
    if errors.As(err, &tplErr) { /* inspect failing line/func */ }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/145b092dd49d3370. Report an issue: GitHub.

Appendix: source

Thrown at kernel/av/calc_template.go:98

// evalRollupTemplate 使用 text/template + sprig 渲染自定义模板统计内容。
// 返回渲染后的字符串;若该字符串可解析为数字则 isNumber 为 true 且 asNumber 为该数值。
// 解析或执行失败时返回 err,由调用方决定如何提示用户。
func evalRollupTemplate(templateContent string, ctx map[string]any) (rendered string, asNumber float64, isNumber bool, err error) {
	if "" == templateContent {
		return
	}

	goTpl := template.New("").Delims(".action{", "}").Funcs(templateFuncMap())
	tpl, parseErr := goTpl.Parse(templateContent)
	if nil != parseErr {
		err = fmt.Errorf("parse template [%s] failed: %s", templateContent, parseErr)
		return
	}

	buf := &bytes.Buffer{}
	if execErr := tpl.Execute(buf, ctx); nil != execErr {
		err = fmt.Errorf("execute template [%s] failed: %s", templateContent, execErr)
		return
	}

	rendered = buf.String()
	if "<no value>" == rendered {
		rendered = ""
		return
	}

	// 渲染结果若可解析为数字,则按数字处理(前端会按列数字格式显示)
	trimmed := strings.TrimSpace(rendered)
	if "" != trimmed {
		if num, parseErr := strconv.ParseFloat(trimmed, 64); nil == parseErr {
			asNumber = num
			isNumber = true
		}
	}
	return

View on GitHub (pinned to 9f775e8a12)