siyuan-note/siyuan · error

renderDocRef is only available when manually inserting or…

Error message

renderDocRef is only available when manually inserting or previewing a template

What it means

renderDocRef on templateDocTreeCollector only works while the collector is 'enabled', i.e. during an explicit manual template insertion or preview render. Any template evaluation outside that guarded lifecycle has no collected document context, so the method refuses to answer queries. This prevents stale or empty doc-ref resolution from arbitrary template renders.

Solutions

  1. Render the template through the manual insert or preview entry point (renderTemplateSource / doc-tree create flow) so the collector is enabled
  2. Check that the code path does not disable the collector between collection and rendering
  3. If you only need static content, remove the renderDocRef action from the template

Example fix

// before: plain template render
model.RenderTemplate(tpl) // collector disabled
// after: use doc-tree aware rendering
model.RenderTemplateSource(box, path, previewMode) // enables collector
Defensive patterns

Strategy: try-catch

Try / catch

out, err := collector.renderDocRef(mode, value)
if err != nil && strings.Contains(err.Error(), "only available when manually inserting") {
  // re-run through the manual insert/preview entry point
}

Prevention

When it happens

Trigger: A template containing a renderDocRef action is rendered through a code path that does not enable the collector (e.g. generic template rendering, an import/export render, or a render triggered after the collection phase already finished).

Common situations: Rendering a doc-tree template via the plain template renderer instead of the doc-tree insertion/preview flow; calling renderDocRef after collector rendering completed and enabled was reset to false; plugin or custom code re-using the collector object outside its intended entry point.

Related errors


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

Appendix: source

Thrown at kernel/model/template_doc_tree.go:243

		return "", fmt.Errorf("createDocTree exceeds the maximum document count of %d", maxTemplateDocTreeDocs)
	}
	collector.bindNodes(nodes, collector.rootID, collector.rootPath, collector.rootHPath)
	collector.nodes = append(collector.nodes, nodes...)
	return "", nil
}

func (collector *templateDocTreeCollector) bindNodes(nodes []*TemplateDocTreeNode, parentID, parentPath, parentHPath string) {
	for _, node := range nodes {
		node.ParentID = parentID
		node.path = strings.TrimSuffix(parentPath, ".sy") + "/" + node.ID + ".sy"
		node.HPath = path.Join(parentHPath, node.Title)
		collector.bindNodes(node.Children, node.ID, node.path, node.HPath)
	}
}

func (collector *templateDocTreeCollector) renderDocRef(mode string, value any) (any, error) {
	if !collector.enabled {
		return nil, errors.New("renderDocRef is only available when manually inserting or previewing a template")
	}
	switch mode {
	case "children":
		id, ok := value.(string)
		if !ok {
			return nil, errors.New("renderDocRef children target must be a document ID")
		}
		if collector.rootID == id {
			return collector.nodes, nil
		}
		for _, node := range flattenTemplateDocTreeNodes0(collector.nodes) {
			if node.ID == id {
				return node.Children, nil
			}
		}
		return []*TemplateDocTreeNode{}, nil
	case "path":
		hPath, ok := value.(string)

View on GitHub (pinned to 9f775e8a12)