commit 0152ebeb911d6fd1fcb6b6603ec29d31c34c4061 Author: Tom Gracey Date: Wed Nov 13 22:56:24 2024 +0000 render method - first version diff --git a/template_nest/nest.go b/template_nest/nest.go new file mode 100644 index 0000000..a9f5f4e --- /dev/null +++ b/template_nest/nest.go @@ -0,0 +1,131 @@ +package template_nest + + +import ( + "encoding/json" + "log" + "regexp" +) + + +type Nest struct { + + templateLabel string + templateDir string + templates map[string]string + tokenDelims [2]string + +} + + +func New( templates map[string]string ) Nest { + + nest := Nest{ + templateLabel: "TEMPLATE", + tokenDelims: [2]string{""}, + templates: templates, + } + + return nest +} + + + + +func (obj *Nest) Render ( sjson []byte ) string { + + var nesti interface{} + err := json.Unmarshal(sjson, &nesti) + if err != nil { + log.Fatal("Failed to parse JSON") + } + + nested := nesti.(map[string]interface{}) + + html := obj._Render( nested ) + + return html + +} + + +func (obj *Nest) _Render( nested interface{} ) string{ + + var html string + + switch vtype := nested.(type) { + + case string: + html = vtype + + case []interface{}: + + html = obj._RenderArray( vtype ) + + case map[string]interface{}: + html = obj._RenderMap( vtype ) + + } + + return html + +} + + +func (obj *Nest) _RenderArray( nested []interface{} ) string { + + html := "" + for v := range nested { + html += obj._Render( v ) + } + + return html +} + + + + +func (obj *Nest) _RenderMap( nested map[string]interface{} ) string { + + templateName := nested[ obj.templateLabel ].(string) + if templateName == "" { + panic("Encountered map with no TEMPLATE label") + } + + template := obj.templates[ templateName ] + + params := make(map[string]string) + + for k, v := range nested { + + if k == obj.templateLabel { + continue + } + + params[ k ] = obj._Render( v ) + + } + + html := obj._FillIn( templateName, template, params ) + + return html + +} + + +func (obj *Nest) _FillIn( templateName string, template string, params map[string]string ) string { + + html := template + for param, val := range params { + + regex, err := regexp.Compile( obj.tokenDelims[0] + `\s*` + param + `\s*` + obj.tokenDelims[1] ) + if err != nil { + panic( err ) + } + + html = regex.ReplaceAllString( html, val ) + + } + + return html +} diff --git a/test_nest.go b/test_nest.go new file mode 100644 index 0000000..a0c18c7 --- /dev/null +++ b/test_nest.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + "tntester/template_nest" +) + +func main(){ + + templates := map[string]string{ + "00-simple-page": ` + + + + + Simple Page + + +

A fairly simple page to test the performance of Template::Nest.

+

+ + + `, "00-simple-component": `

`} + + json := []byte(`{ + "TEMPLATE": "00-simple-page", + "variable": "Simple Variable", + "simple_component": { + "TEMPLATE":"01-simple-component", + "variable": "Simple Variable in Simple Component" + } + }`) + + + nest := template_nest.New( templates ) + + html := nest.Render( json ) + + fmt.Println( html ) + + +}