Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Add template type column to Attribute View https://github.com/siyuan-note/siyuan/issues/8766

Daniel4fdd0ddef046
diff --git a/kernel/av/av.go b/kernel/av/av.go
index 08e370de17..3ee8d7186c 100644
--- a/kernel/av/av.go
+++ b/kernel/av/av.go
@@ -354,8 +354,8 @@ type ValueTemplate struct {
Content string `json:"content"`
}
-func (t *ValueTemplate) Render(blockID string, r func(blockID string) string) {
- t.Content = r(blockID)
+func (t *ValueTemplate) Render(blockID, tplContent string, r func(blockID, tplContent string) string) {
+ t.Content = r(blockID, tplContent)
}
// View 描述了视图的结构。
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index d5814b7c50..e290227206 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -38,6 +38,28 @@ type BlockAttributeViewKeys struct {
KeyValues []*av.KeyValues `json:"keyValues"`
}
+func renderTemplateCol(blockID, tplContent string) string {
+ funcMap := sprig.TxtFuncMap()
+ goTpl := template.New("").Delims(".action{", "}")
+ tplContent = strings.ReplaceAll(tplContent, ".custom-", ".custom_") // 模板中的属性名不允许包含 - 字符,因此这里需要替换
+ tpl, tplErr := goTpl.Funcs(funcMap).Parse(tplContent)
+ if nil != tplErr {
+ logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr)
+ return ""
+ }
+
+ buf := &bytes.Buffer{}
+ ial := GetBlockAttrs(blockID)
+ dataModel := map[string]string{} // 复制一份 IAL 以避免修改原始数据
+ for k, v := range ial {
+ dataModel[strings.ReplaceAll(k, "custom-", "custom_")] = v
+ }
+ if err := tpl.Execute(buf, dataModel); nil != err {
+ logging.LogWarnf("execute template [%s] failed: %s", tplContent, err)
+ }
+ return buf.String()
+}
+
func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
waitForSyncingStorages()
@@ -74,6 +96,14 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
}
}
+ if av.KeyTypeTemplate == kValues.Key.Type {
+ // 渲染模板列
+ content := renderTemplateCol(blockID, kValues.Key.Template)
+ if "<no value>" != content {
+ kValues.Values = append(kValues.Values, &av.Value{ID: ast.NewNodeID(), KeyID: kValues.Key.ID, BlockID: blockID, Type: av.KeyTypeTemplate, Template: &av.ValueTemplate{Content: content}})
+ }
+ }
+
if 0 < len(kValues.Values) {
keyValues = append(keyValues, kValues)
}
@@ -233,31 +263,8 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *a
// 渲染模板列
if av.KeyTypeTemplate == tableCell.ValueType {
- render := func(blockID string) string {
- funcMap := sprig.TxtFuncMap()
- goTpl := template.New("").Delims(".action{", "}")
- tplContent := col.Template
- tplContent = strings.ReplaceAll(tplContent, ".custom-", ".custom_") // 模板中的属性名不允许包含 - 字符,因此这里需要替换
- tpl, tplErr := goTpl.Funcs(funcMap).Parse(tplContent)
- if nil != tplErr {
- logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr)
- return ""
- }
-
- buf := &bytes.Buffer{}
- ial := GetBlockAttrs(blockID)
- dataModel := map[string]string{} // 复制一份 IAL 以避免修改原始数据
- for k, v := range ial {
- dataModel[strings.ReplaceAll(k, "custom-", "custom_")] = v
- }
- if err = tpl.Execute(buf, dataModel); nil != err {
- logging.LogWarnf("execute template [%s] failed: %s", tplContent, err)
- }
- return buf.String()
- }
-
tableCell.Value = &av.Value{ID: tableCell.ID, KeyID: col.ID, BlockID: rowID, Type: av.KeyTypeTemplate, Template: &av.ValueTemplate{}}
- tableCell.Value.Template.Render(tableCell.Value.BlockID, render)
+ tableCell.Value.Template.Render(tableCell.Value.BlockID, col.Template, renderTemplateCol)
}
tableRow.Cells = append(tableRow.Cells, tableCell)