siyuan-note/siyuan · error

parse template [ ] failed

Error message

parse template [%s] failed: %s

What it means

Formatted error raised in evalRollupTemplate when Go text/template parsing of a rollup template (delims .action{ }) fails. The message embeds the offending template content and the underlying parse error, so a syntax error in the template body is directly visible.

Solutions

  1. Fix the template syntax shown in the message — check every .action{ has a matching }
  2. Test the template in the SiYuan template editor before saving the rollup
  3. Escape or strip stray braces from programmatically generated template content

Example fix

// before
tpl := ".action{.foo }{.bar}" // unbalanced
// after
tpl := ".action{.foo}.action{.bar}" // balanced delimiters
Defensive patterns

Strategy: try-catch

Validate before calling

_, perr := template.New("").Delims(".action{", "}").Parse(tplContent)
if perr != nil {
    return fmt.Errorf("template syntax invalid: %v", perr)
}

Try / catch

if _, err := evalRollupTemplate(tplContent, ctx); err != nil {
    if strings.HasPrefix(err.Error(), "parse template") {
        // show err to user: it embeds the bad template and parse reason
    }
}

Prevention

When it happens

Trigger: calcFieldRollup/calcFieldByTemplate evaluating a rollup whose template content contains invalid template syntax — unbalanced .action{ }, wrong pipe/field syntax, or characters that break the Go template lexer.

Common situations: User-typed template with a typo like .action{.value} vs .action{.value}; missing closing brace after editing; copying a template from docs where the delimiter got mangled; programmatic generation interpolating raw text containing "}".

Related errors


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

Appendix: source

Thrown at kernel/av/calc_template.go:92

		ctx["min"] = float64(0)
		ctx["max"] = float64(0)
		ctx["median"] = float64(0)
	}
	return ctx
}

// 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 {

View on GitHub (pinned to 9f775e8a12)