~repos /gromer
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.
9c842f33
—
Peter John 3 years ago
Update Readme
- handlebars/README.md +132 -0
- readme.md +2 -134
handlebars/README.md
CHANGED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Templating
|
|
2
|
+
|
|
3
|
+
Gromer uses a handlebars like templating language for components and pages. This is a modified version of this package [velvet](https://github.com/gobuffalo/velvet)
|
|
4
|
+
If you know handlebars, you basically know how to use it.
|
|
5
|
+
|
|
6
|
+
You can install this plugin [VSCode Go inline html plugin](https://marketplace.visualstudio.com/items?itemName=pyros2097.vscode-go-inline-html) for syntax highlighting the templates.
|
|
7
|
+
|
|
8
|
+
Let's assume you have a template (a string of some kind):
|
|
9
|
+
|
|
10
|
+
```handlebars
|
|
11
|
+
<!-- some input -->
|
|
12
|
+
<h1>{{name}}</h1>
|
|
13
|
+
<ul>
|
|
14
|
+
{{#each names}}
|
|
15
|
+
<li>{{@value}}</li>
|
|
16
|
+
{{/each}}
|
|
17
|
+
</ul>
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Given that string, you can render the template like such:
|
|
21
|
+
|
|
22
|
+
```html
|
|
23
|
+
<h1>Mark</h1>
|
|
24
|
+
<ul>
|
|
25
|
+
<li>John</li>
|
|
26
|
+
<li>Paul</li>
|
|
27
|
+
<li>George</li>
|
|
28
|
+
<li>Ringo</li>
|
|
29
|
+
</ul>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### If Statements
|
|
33
|
+
|
|
34
|
+
```handlebars
|
|
35
|
+
{{#if true}}
|
|
36
|
+
render this
|
|
37
|
+
{{/if}}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
#### Else Statements
|
|
41
|
+
|
|
42
|
+
```handlebars
|
|
43
|
+
{{#if false}}
|
|
44
|
+
won't render this
|
|
45
|
+
{{else}}
|
|
46
|
+
render this
|
|
47
|
+
{{/if}}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
#### Unless Statements
|
|
51
|
+
|
|
52
|
+
```handlebars
|
|
53
|
+
{{#unless true}}
|
|
54
|
+
won't render this
|
|
55
|
+
{{/unless}}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Each Statements
|
|
59
|
+
|
|
60
|
+
#### Arrays
|
|
61
|
+
|
|
62
|
+
When looping through `arrays` or `slices`, the block being looped through will be access to the "global" context, as well as have four new variables available within that block:
|
|
63
|
+
|
|
64
|
+
- `@first` [`bool`] - is this the first pass through the iteration?
|
|
65
|
+
- `@last` [`bool`] - is this the last pass through the iteration?
|
|
66
|
+
- `@index` [`int`] - the counter of where in the loop you are, starting with `0`.
|
|
67
|
+
- `@value` - the current element in the array or slice that is being iterated over.
|
|
68
|
+
|
|
69
|
+
```handlebars
|
|
70
|
+
<ul>
|
|
71
|
+
{{#each names}}
|
|
72
|
+
<li>{{@index}} - {{@value}}</li>
|
|
73
|
+
{{/each}}
|
|
74
|
+
</ul>
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
By using "block parameters" you can change the "key" of the element being accessed from `@value` to a key of your choosing.
|
|
78
|
+
|
|
79
|
+
```handlebars
|
|
80
|
+
<ul>
|
|
81
|
+
{{#each names as |name|}}
|
|
82
|
+
<li>{{name}}</li>
|
|
83
|
+
{{/each}}
|
|
84
|
+
</ul>
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
To change both the key and the index name you can pass two "block parameters"; the first being the new name for the index and the second being the name for the element.
|
|
88
|
+
|
|
89
|
+
```handlebars
|
|
90
|
+
<ul>
|
|
91
|
+
{{#each names as |index, name|}}
|
|
92
|
+
<li>{{ index }} - {{ name }}</li>
|
|
93
|
+
{{/each}}
|
|
94
|
+
</ul>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### Maps
|
|
98
|
+
|
|
99
|
+
Looping through `maps` using the `each` helper is also supported, and follows very similar guidelines to looping through `arrays`.
|
|
100
|
+
|
|
101
|
+
- `@first` [`bool`] - is this the first pass through the iteration?
|
|
102
|
+
- `@last` [`bool`] - is this the last pass through the iteration?
|
|
103
|
+
- `@key` - the key of the pair being accessed.
|
|
104
|
+
- `@value` - the value of the pair being accessed.
|
|
105
|
+
|
|
106
|
+
```handlebars
|
|
107
|
+
<ul>
|
|
108
|
+
{{#each users}}
|
|
109
|
+
<li>{{@key}} - {{@value}}</li>
|
|
110
|
+
{{/each}}
|
|
111
|
+
</ul>
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
By using "block parameters" you can change the "key" of the element being accessed from `@value` to a key of your choosing.
|
|
115
|
+
|
|
116
|
+
```handlebars
|
|
117
|
+
<ul>
|
|
118
|
+
{{#each users as |user|}}
|
|
119
|
+
<li>{{@key}} - {{user}}</li>
|
|
120
|
+
{{/each}}
|
|
121
|
+
</ul>
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
To change both the key and the value name you can pass two "block parameters"; the first being the new name for the key and the second being the name for the value.
|
|
125
|
+
|
|
126
|
+
```handlebars
|
|
127
|
+
<ul>
|
|
128
|
+
{{#each users as |key, user|}}
|
|
129
|
+
<li>{{ key }} - {{ user }}</li>
|
|
130
|
+
{{/each}}
|
|
131
|
+
</ul>
|
|
132
|
+
```
|
readme.md
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
It uses a declarative syntax using inline handlebar templates for components and pages.
|
|
7
7
|
It also generates http handlers for your routes which follow a particular folder structure. Similar to other frameworks like nextjs, sveltekit.
|
|
8
8
|
These handlers are also normal functions and can be imported in other packages directly. ((inspired by [Encore](https://encore.dev/)).
|
|
9
|
+
More information on the templating syntax is given [here](https://github.com/pyrossh/gromer/blob/master/handlebars/README.md),
|
|
9
10
|
|
|
10
11
|
# Requirements
|
|
11
12
|
|
|
@@ -217,137 +218,4 @@ func main() {
|
|
|
217
218
|
log.Fatal().Stack().Err(err).Msg("failed to listen")
|
|
218
219
|
}
|
|
219
220
|
}
|
|
220
|
-
```
|
|
221
|
+
```
|
|
221
|
-
|
|
222
|
-
# Templating
|
|
223
|
-
|
|
224
|
-
Gromer uses a handlebars like templating language for components and pages. This is a modified version of this package [velvet](https://github.com/gobuffalo/velvet)
|
|
225
|
-
If you know handlebars, you basically know how to use it.
|
|
226
|
-
|
|
227
|
-
You can install this plugin [VSCode Go inline html plugin](https://marketplace.visualstudio.com/items?itemName=pyros2097.vscode-go-inline-html) for syntax highlighting the templates.
|
|
228
|
-
|
|
229
|
-
Let's assume you have a template (a string of some kind):
|
|
230
|
-
|
|
231
|
-
```handlebars
|
|
232
|
-
<!-- some input -->
|
|
233
|
-
<h1>{{name}}</h1>
|
|
234
|
-
<ul>
|
|
235
|
-
{{#each names}}
|
|
236
|
-
<li>{{@value}}</li>
|
|
237
|
-
{{/each}}
|
|
238
|
-
</ul>
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
Given that string, you can render the template like such:
|
|
242
|
-
|
|
243
|
-
```html
|
|
244
|
-
<h1>Mark</h1>
|
|
245
|
-
<ul>
|
|
246
|
-
<li>John</li>
|
|
247
|
-
<li>Paul</li>
|
|
248
|
-
<li>George</li>
|
|
249
|
-
<li>Ringo</li>
|
|
250
|
-
</ul>
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
### If Statements
|
|
254
|
-
|
|
255
|
-
```handlebars
|
|
256
|
-
{{#if true}}
|
|
257
|
-
render this
|
|
258
|
-
{{/if}}
|
|
259
|
-
```
|
|
260
|
-
|
|
261
|
-
#### Else Statements
|
|
262
|
-
|
|
263
|
-
```handlebars
|
|
264
|
-
{{#if false}}
|
|
265
|
-
won't render this
|
|
266
|
-
{{else}}
|
|
267
|
-
render this
|
|
268
|
-
{{/if}}
|
|
269
|
-
```
|
|
270
|
-
|
|
271
|
-
#### Unless Statements
|
|
272
|
-
|
|
273
|
-
```handlebars
|
|
274
|
-
{{#unless true}}
|
|
275
|
-
won't render this
|
|
276
|
-
{{/unless}}
|
|
277
|
-
```
|
|
278
|
-
|
|
279
|
-
### Each Statements
|
|
280
|
-
|
|
281
|
-
#### Arrays
|
|
282
|
-
|
|
283
|
-
When looping through `arrays` or `slices`, the block being looped through will be access to the "global" context, as well as have four new variables available within that block:
|
|
284
|
-
|
|
285
|
-
- `@first` [`bool`] - is this the first pass through the iteration?
|
|
286
|
-
- `@last` [`bool`] - is this the last pass through the iteration?
|
|
287
|
-
- `@index` [`int`] - the counter of where in the loop you are, starting with `0`.
|
|
288
|
-
- `@value` - the current element in the array or slice that is being iterated over.
|
|
289
|
-
|
|
290
|
-
```handlebars
|
|
291
|
-
<ul>
|
|
292
|
-
{{#each names}}
|
|
293
|
-
<li>{{@index}} - {{@value}}</li>
|
|
294
|
-
{{/each}}
|
|
295
|
-
</ul>
|
|
296
|
-
```
|
|
297
|
-
|
|
298
|
-
By using "block parameters" you can change the "key" of the element being accessed from `@value` to a key of your choosing.
|
|
299
|
-
|
|
300
|
-
```handlebars
|
|
301
|
-
<ul>
|
|
302
|
-
{{#each names as |name|}}
|
|
303
|
-
<li>{{name}}</li>
|
|
304
|
-
{{/each}}
|
|
305
|
-
</ul>
|
|
306
|
-
```
|
|
307
|
-
|
|
308
|
-
To change both the key and the index name you can pass two "block parameters"; the first being the new name for the index and the second being the name for the element.
|
|
309
|
-
|
|
310
|
-
```handlebars
|
|
311
|
-
<ul>
|
|
312
|
-
{{#each names as |index, name|}}
|
|
313
|
-
<li>{{ index }} - {{ name }}</li>
|
|
314
|
-
{{/each}}
|
|
315
|
-
</ul>
|
|
316
|
-
```
|
|
317
|
-
|
|
318
|
-
#### Maps
|
|
319
|
-
|
|
320
|
-
Looping through `maps` using the `each` helper is also supported, and follows very similar guidelines to looping through `arrays`.
|
|
321
|
-
|
|
322
|
-
- `@first` [`bool`] - is this the first pass through the iteration?
|
|
323
|
-
- `@last` [`bool`] - is this the last pass through the iteration?
|
|
324
|
-
- `@key` - the key of the pair being accessed.
|
|
325
|
-
- `@value` - the value of the pair being accessed.
|
|
326
|
-
|
|
327
|
-
```handlebars
|
|
328
|
-
<ul>
|
|
329
|
-
{{#each users}}
|
|
330
|
-
<li>{{@key}} - {{@value}}</li>
|
|
331
|
-
{{/each}}
|
|
332
|
-
</ul>
|
|
333
|
-
```
|
|
334
|
-
|
|
335
|
-
By using "block parameters" you can change the "key" of the element being accessed from `@value` to a key of your choosing.
|
|
336
|
-
|
|
337
|
-
```handlebars
|
|
338
|
-
<ul>
|
|
339
|
-
{{#each users as |user|}}
|
|
340
|
-
<li>{{@key}} - {{user}}</li>
|
|
341
|
-
{{/each}}
|
|
342
|
-
</ul>
|
|
343
|
-
```
|
|
344
|
-
|
|
345
|
-
To change both the key and the value name you can pass two "block parameters"; the first being the new name for the key and the second being the name for the value.
|
|
346
|
-
|
|
347
|
-
```handlebars
|
|
348
|
-
<ul>
|
|
349
|
-
{{#each users as |key, user|}}
|
|
350
|
-
<li>{{ key }} - {{ user }}</li>
|
|
351
|
-
{{/each}}
|
|
352
|
-
</ul>
|
|
353
|
-
```
|