~repos /gromer

#golang#htmx#ssr

git clone https://pyrossh.dev/repos/gromer.git

gromer is a framework and cli to build multipage web apps in golang using htmx and alpinejs.


cb22233c Peter John

3 years ago
add children
Files changed (2) hide show
  1. gsx/template.go +26 -16
  2. gsx/template_test.go +7 -1
gsx/template.go CHANGED
@@ -16,7 +16,7 @@ type Html map[string]interface{}
16
16
 
17
17
  func (h Html) Render(tpl string) string {
18
18
  tree := &Module{}
19
- err := xmlParser.ParseBytes(tpl, []byte(tpl), tree)
19
+ err := xmlParser.ParseBytes(tpl[0:10], []byte(tpl), tree)
20
20
  if err != nil {
21
21
  panic(err)
22
22
  }
@@ -70,6 +70,20 @@ func getAttribute(k string, kvs []*Attribute) string {
70
70
  return ""
71
71
  }
72
72
 
73
+ func convert(ref string, i interface{}) interface{} {
74
+ switch iv := i.(type) {
75
+ case bool:
76
+ if strings.Contains(ref, "!") {
77
+ return !iv
78
+ } else {
79
+ return iv
80
+ }
81
+ case string:
82
+ return iv
83
+ }
84
+ return nil
85
+ }
86
+
73
87
  func subsRef(ctx map[string]interface{}, ref string) interface{} {
74
88
  if f, ok := funcMap[ref]; ok {
75
89
  return f.(func() string)()
@@ -78,20 +92,11 @@ func subsRef(ctx map[string]interface{}, ref string) interface{} {
78
92
  if len(parts) == 2 {
79
93
  if v, ok := ctx[parts[0]]; ok {
80
94
  i := reflect.ValueOf(v).Elem().FieldByName(parts[1]).Interface()
81
- switch iv := i.(type) {
82
- case bool:
83
- if strings.Contains(ref, "!") {
84
- return !iv
95
+ return convert(ref, i)
85
- } else {
86
- return iv
87
- }
88
- case string:
89
- return iv
90
- }
91
96
  }
92
97
  }
98
+ return convert(ref, ctx[ref])
93
99
  }
94
- return nil
95
100
  }
96
101
 
97
102
  func render(x *Xml, ctx map[string]interface{}) string {
@@ -122,6 +127,8 @@ func render(x *Xml, ctx map[string]interface{}) string {
122
127
  if x.Value != nil {
123
128
  if x.Value.Ref != "" {
124
129
  s += space + " " + subsRef(ctx, x.Value.Ref).(string) + "\n"
130
+ } else if x.Value.Str != "" {
131
+ s += space + " " + strings.ReplaceAll(x.Value.Str, `"`, "") + "\n"
125
132
  }
126
133
  }
127
134
  if x.Name == "For" {
@@ -150,6 +157,9 @@ func render(x *Xml, ctx map[string]interface{}) string {
150
157
  args = append(args, reflect.ValueOf(v))
151
158
  }
152
159
  }
160
+ if len(x.Children) > 0 {
161
+ h["children"] = render(x.Children[0], h)
162
+ }
153
163
  result := reflect.ValueOf(comp.Func).Call(args)
154
164
  s += result[0].Interface().(string) + "\n"
155
165
  } else {
@@ -162,10 +172,10 @@ func render(x *Xml, ctx map[string]interface{}) string {
162
172
  if !found {
163
173
  panic(fmt.Errorf("Comp not found %s", x.Name))
164
174
  }
175
+ for _, c := range x.Children {
176
+ ctx["_space"] = space + " "
177
+ s += render(c, ctx) + "\n"
165
- }
178
+ }
166
- for _, c := range x.Children {
167
- ctx["_space"] = space + " "
168
- s += render(c, ctx) + "\n"
169
179
  }
170
180
  }
171
181
  s += space + "</" + x.Name + ">"
gsx/template_test.go CHANGED
@@ -18,6 +18,7 @@ func Todo(h Html, todo *TodoData) string {
18
18
  <div class="view">
19
19
  <span>{todo.Text}</span>
20
20
  </div>
21
+ {children}
21
22
  </li>
22
23
  `)
23
24
  }
@@ -39,7 +40,9 @@ func TestHtml(t *testing.T) {
39
40
  actual := Html(ctx).Render(`
40
41
  <ul id="todo-list" class="relative">
41
42
  <For key="todos" itemKey="todo">
43
+ <Todo key="todo">
42
- <Todo key="todo"></Todo>
44
+ <div>"Todo123"</div>
45
+ </Todo>
43
46
  </For>
44
47
  <span>{WebsiteName}</span>
45
48
  </ul>
@@ -48,6 +51,9 @@ func TestHtml(t *testing.T) {
48
51
  <For key="todos" itemKey="todo">
49
52
  <Todo key="todo">
50
53
  <li id="b1a7359c-ebb4-11ec-8ea0-0242ac120002" class="completed">
54
+ <div>
55
+ Todo123
56
+ </div>
51
57
  <div class="view">
52
58
  <span>
53
59
  My first todo