-
Notifications
You must be signed in to change notification settings - Fork 5
/
ContentRenderBlocks.go
70 lines (52 loc) · 1.41 KB
/
ContentRenderBlocks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package cms
import (
"strings"
)
// RenderBlocks renders the blocks in a string
func (cms *Cms) ContentRenderBlocks(content string) (string, error) {
blockIDs := ContentFindIdsByPatternPrefix(content, "BLOCK")
var err error
for _, blockID := range blockIDs {
content, err = cms.ContentRenderBlockByID(content, blockID)
if err != nil {
return content, err
}
}
return content, nil
}
// ContentRenderBlockByID renders the block specified by the ID in a content
// if the blockID is empty or not found the initial content is returned
func (cms *Cms) ContentRenderBlockByID(content string, blockID string) (string, error) {
if blockID == "" {
return content, nil
}
blockContent, err := cms.findBlockContent(blockID)
if err != nil {
return "", err
}
content = strings.ReplaceAll(content, "[[BLOCK_"+blockID+"]]", blockContent)
content = strings.ReplaceAll(content, "[[ BLOCK_"+blockID+" ]]", blockContent)
return content, nil
}
func (cms *Cms) findBlockContent(blockID string) (string, error) {
block, err := cms.BlockFindByID(blockID)
if err != nil {
return "", err
}
var blockContent string
if block == nil {
blockContent = ""
} else {
blockStatus, err := block.GetString("status", "")
if err != nil {
return "", err
}
if blockStatus == "active" {
blockContent, err = block.GetString("content", "")
if err != nil {
return "", err
}
}
}
return blockContent, nil
}