{"record":{"id":"17d6291e2b63c844","repo":"siyuan-note/siyuan","slug":"renderdocref-is-only-available-when-manually-inser","errorCode":null,"errorMessage":"renderDocRef is only available when manually inserting or previewing a template","messagePattern":"renderDocRef is only available when manually inserting or previewing a template","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_doc_tree.go","lineNumber":243,"sourceCode":"\t\treturn \"\", fmt.Errorf(\"createDocTree exceeds the maximum document count of %d\", maxTemplateDocTreeDocs)\n\t}\n\tcollector.bindNodes(nodes, collector.rootID, collector.rootPath, collector.rootHPath)\n\tcollector.nodes = append(collector.nodes, nodes...)\n\treturn \"\", nil\n}\n\nfunc (collector *templateDocTreeCollector) bindNodes(nodes []*TemplateDocTreeNode, parentID, parentPath, parentHPath string) {\n\tfor _, node := range nodes {\n\t\tnode.ParentID = parentID\n\t\tnode.path = strings.TrimSuffix(parentPath, \".sy\") + \"/\" + node.ID + \".sy\"\n\t\tnode.HPath = path.Join(parentHPath, node.Title)\n\t\tcollector.bindNodes(node.Children, node.ID, node.path, node.HPath)\n\t}\n}\n\nfunc (collector *templateDocTreeCollector) renderDocRef(mode string, value any) (any, error) {\n\tif !collector.enabled {\n\t\treturn nil, errors.New(\"renderDocRef is only available when manually inserting or previewing a template\")\n\t}\n\tswitch mode {\n\tcase \"children\":\n\t\tid, ok := value.(string)\n\t\tif !ok {\n\t\t\treturn nil, errors.New(\"renderDocRef children target must be a document ID\")\n\t\t}\n\t\tif collector.rootID == id {\n\t\t\treturn collector.nodes, nil\n\t\t}\n\t\tfor _, node := range flattenTemplateDocTreeNodes0(collector.nodes) {\n\t\t\tif node.ID == id {\n\t\t\t\treturn node.Children, nil\n\t\t\t}\n\t\t}\n\t\treturn []*TemplateDocTreeNode{}, nil\n\tcase \"path\":\n\t\thPath, ok := value.(string)","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_doc_tree.go#L225-L261","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Render the template through the manual insert or preview entry point (renderTemplateSource / doc-tree create flow) so the collector is enabled","Check that the code path does not disable the collector between collection and rendering","If you only need static content, remove the renderDocRef action from the template"],"exampleFix":"// before: plain template render\nmodel.RenderTemplate(tpl) // collector disabled\n// after: use doc-tree aware rendering\nmodel.RenderTemplateSource(box, path, previewMode) // enables collector","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"out, err := collector.renderDocRef(mode, value)\nif err != nil && strings.Contains(err.Error(), \"only available when manually inserting\") {\n  // re-run through the manual insert/preview entry point\n}","preventionTips":["Only render doc-tree templates via the supported insert/preview flows","Do not reuse a collector after its render completed","Avoid renderDocRef in templates meant for generic rendering"],"tags":["go","template","invalid-state"],"backgroundTag":"feature-not-enabled","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"}