github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/common/templates/context.go (about) 1 package tmpl 2 3 import ( 4 "reflect" 5 ) 6 7 // For use in generated code 8 type FragLite struct { 9 Body string 10 } 11 12 type Fragment struct { 13 Body string 14 TemplateName string 15 Index int 16 Seen bool 17 } 18 19 type OutBufferFrame struct { 20 Body string 21 Type string 22 TemplateName string 23 Extra interface{} 24 Extra2 interface{} 25 } 26 27 type CContext struct { 28 RootHolder string 29 VarHolder string 30 HoldReflect reflect.Value 31 RootTemplateName string 32 TemplateName string 33 LoopDepth int 34 OutBuf *[]OutBufferFrame 35 } 36 37 func (con *CContext) Push(nType string, body string) (index int) { 38 *con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, nType, con.TemplateName, nil, nil}) 39 return con.LastBufIndex() 40 } 41 42 func (con *CContext) PushText(body string, fragIndex int, fragOutIndex int) (index int) { 43 *con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, "text", con.TemplateName, fragIndex, fragOutIndex}) 44 return con.LastBufIndex() 45 } 46 47 func (con *CContext) PushPhrase(langIndex int) (index int) { 48 *con.OutBuf = append(*con.OutBuf, OutBufferFrame{"", "lang", con.TemplateName, langIndex, nil}) 49 return con.LastBufIndex() 50 } 51 52 func (con *CContext) PushPhrasef(langIndex int, args string) (index int) { 53 *con.OutBuf = append(*con.OutBuf, OutBufferFrame{args, "langf", con.TemplateName, langIndex, nil}) 54 return con.LastBufIndex() 55 } 56 57 func (con *CContext) StartIf(body string) (index int) { 58 *con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, "startif", con.TemplateName, false, nil}) 59 return con.LastBufIndex() 60 } 61 func (con *CContext) StartIfPtr(body string) (index int) { 62 *con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, "startif", con.TemplateName, true, nil}) 63 return con.LastBufIndex() 64 } 65 66 func (con *CContext) EndIf(startIndex int, body string) (index int) { 67 *con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, "endif", con.TemplateName, startIndex, nil}) 68 return con.LastBufIndex() 69 } 70 71 func (con *CContext) StartLoop(body string) (index int) { 72 con.LoopDepth++ 73 return con.Push("startloop", body) 74 } 75 76 func (con *CContext) EndLoop(body string) (index int) { 77 return con.Push("endloop", body) 78 } 79 80 func (con *CContext) StartTemplate(body string) (index int) { 81 return con.addFrame(body, "starttemplate", nil, nil) 82 } 83 84 func (con *CContext) EndTemplate(body string) (index int) { 85 return con.Push("endtemplate", body) 86 } 87 88 func (con *CContext) AttachVars(vars string, index int) { 89 outBuf := *con.OutBuf 90 n := outBuf[index] 91 if n.Type != "starttemplate" && n.Type != "startloop" && n.Type != "startif" { 92 panic("not a starttemplate, startloop or startif node") 93 } 94 n.Body += vars 95 outBuf[index] = n 96 } 97 98 func (con *CContext) addFrame(body, ftype string, extra1 interface{}, extra2 interface{}) (index int) { 99 *con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, ftype, con.TemplateName, extra1, extra2}) 100 return con.LastBufIndex() 101 } 102 103 func (con *CContext) LastBufIndex() int { 104 return len(*con.OutBuf) - 1 105 } 106 107 func (con *CContext) DiscardAndAfter(index int) { 108 outBuf := *con.OutBuf 109 if len(outBuf) <= index { 110 return 111 } 112 if index == 0 { 113 outBuf = nil 114 } else { 115 outBuf = outBuf[:index] 116 } 117 *con.OutBuf = outBuf 118 }