gosora/common/templates/context.go
Azareal 52c8be4173 Variables which are computed multiple times in templates should only be computed once now.
Enabled the Watchdog Goroutine.
Moved a couple of parser tests into their own file.
Eliminated the hard-coded URLs in TestParser()
Refactored the alert system so that a specific actor is only loaded once rather than once for each alert. This isn't a problem in practice due to the user cache, but it might be on high traffic sites.
Don't run HandleServerSync() on single server setups.
Don't load the user in GetTopicUser(), if the topic creator is the current user.
Fixed a bug in the template generator where endloop nodes were being pushed instead of endif ones.
2018-11-22 17:21:43 +10:00

77 lines
1.8 KiB
Go

package tmpl
import (
"reflect"
)
// For use in generated code
type FragLite struct {
Body string
}
type Fragment struct {
Body string
TemplateName string
Index int
Seen bool
}
type OutBufferFrame struct {
Body string
Type string
TemplateName string
Extra interface{}
Extra2 interface{}
}
type CContext struct {
VarHolder string
HoldReflect reflect.Value
TemplateName string
LoopDepth int
OutBuf *[]OutBufferFrame
}
func (con *CContext) Push(nType string, body string) (index int) {
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, nType, con.TemplateName, nil, nil})
return len(*con.OutBuf) - 1
}
func (con *CContext) PushText(body string, fragIndex int, fragOutIndex int) (index int) {
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, "text", con.TemplateName, fragIndex, fragOutIndex})
return len(*con.OutBuf) - 1
}
func (con *CContext) PushPhrase(body string, langIndex int) (index int) {
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, "lang", con.TemplateName, langIndex, nil})
return len(*con.OutBuf) - 1
}
func (con *CContext) StartLoop(body string) (index int) {
con.LoopDepth++
return con.Push("startloop", body)
}
func (con *CContext) EndLoop(body string) (index int) {
return con.Push("endloop", body)
}
func (con *CContext) StartTemplate(body string) (index int) {
*con.OutBuf = append(*con.OutBuf, OutBufferFrame{body, "starttemplate", con.TemplateName, nil, nil})
return len(*con.OutBuf) - 1
}
func (con *CContext) EndTemplate(body string) (index int) {
return con.Push("endtemplate", body)
}
func (con *CContext) AttachVars(vars string, index int) {
outBuf := *con.OutBuf
node := outBuf[index]
if node.Type != "starttemplate" && node.Type != "startloop" {
panic("not a starttemplate node")
}
node.Body += vars
outBuf[index] = node
}