Skip to content

Commit 2f9f0ef

Browse files
committed
added global context, functions and importing
1 parent 4097cf1 commit 2f9f0ef

14 files changed

Lines changed: 538 additions & 103 deletions

File tree

.github/templates/README.template.md

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Goplater uses Go's [builtin templating library](https://pkg.go.dev/text/template
3131
File Content: $​{​{​{ read "./myfile.txt" }​}​}
3232
```
3333

34-
### Functions
34+
### Simple Functions
3535

3636
As you saw in the example above `read` is used for reading and output file contents.
3737
But there are more as you will see in the following…
@@ -161,6 +161,14 @@ Joins strings with separator.
161161
join "Apple" "Banana" "Strawberry" ", "
162162
```
163163

164+
#### `append`
165+
166+
Appends another string to string.
167+
168+
```
169+
append "Hello " "World!"
170+
```
171+
164172
#### `regexMatch`
165173

166174
Outputs wether string matches regex.
@@ -198,17 +206,109 @@ regexReplace "string" "replace_regex" "replace_with"
198206
Deletes an entry from a dictionary or array.
199207

200208
```
201-
202209
delete map "key"
210+
```
211+
212+
```
213+
delete array 0
214+
```
215+
216+
#### `set`
203217

218+
Sets key in dictionary or array to value.
219+
220+
```
221+
set map "key" value
204222
```
205223

224+
```
225+
set array 0 value
206226
```
207227

208-
delete array 0
228+
#### `push`
229+
230+
Pushes value onto top of array.
231+
232+
```
233+
push array value
234+
```
235+
236+
### Advanced Functions
237+
238+
In addition to the function in the [Simple Functions](#simple-functions) section, there are also some functions for advanced usage.
239+
240+
#### `import`
241+
242+
Imports a file and executes it as template, output is discarded.
243+
244+
```
245+
import "functions.inc.gtmpl"
246+
```
209247

248+
#### `globalSet`
249+
250+
Sets key globally to value.
251+
252+
```
253+
globalSet "key" value
210254
```
211255

256+
#### `globalGet`
257+
258+
Returns global value at key.
259+
260+
#### `funcDefine`
261+
262+
Defines a global function.
263+
264+
```
265+
funcDefine "name" "{{{ return 0 "Hello World" }}}"
266+
```
267+
268+
The second argument is the template body, notice the `{{{ ... }}}` instead of `${{{ ... }}}`.
269+
270+
> [!WARNING]
271+
> You may need to escape some characters like `"` with `\`
272+
273+
Raw output is discarded only output via [`return`](#return) persists.
274+
275+
##### `return`
276+
277+
> [!WARNING]
278+
> This function is **only** accessible from within functions!
279+
280+
Sets return argument at index to value.
281+
282+
```
283+
funcDefine "helloWorld" "{{{ return 0 "Hello World!" }}}"
284+
```
285+
286+
Overwriting previous return arguments is possible.
287+
288+
#### `funcCall`
289+
290+
Calls a global function by its name (without passing any arguments).
291+
292+
```
293+
294+
funcCall "name"
295+
296+
```
297+
298+
Returns list of [`return`](#return) outputs in order of index.
299+
300+
#### `funcCallArgs`
301+
302+
Same as [`funcCall`](#funccall), but arguments can be passed.
303+
304+
```
305+
306+
funcCallArgs "name" arg1 arg2
307+
308+
```
309+
310+
Arguments are accessible in function body with `{{{ index .args 0 }}}`.
311+
212312
## Contributing
213313

214314
Found a bug or just want to change or add something?
@@ -226,3 +326,11 @@ This Project is licensed under the [MIT License](./LICENSE).
226326
## Legal
227327

228328
Logo designed by [@CodeShellDev](https://github.com/codeshelldev) — All Rights Reserved. Go gopher mascot originally created by [Renée French](https://instagram.com/reneefrench/), used under the [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) license.
329+
330+
```
331+
332+
```
333+
334+
```
335+
336+
```

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require github.com/codeshelldev/gotl/pkg/stringutils v0.0.8 // indirect
1818

1919
require (
2020
github.com/andybalholm/cascadia v1.3.3 // indirect
21+
github.com/google/uuid v1.6.0
2122
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2223
github.com/spf13/pflag v1.0.9 // indirect
2324
golang.org/x/net v0.47.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/codeshelldev/gotl/pkg/templating v0.0.16 h1:0dl/NEApCtlm4kyEscQPknx4D
1010
github.com/codeshelldev/gotl/pkg/templating v0.0.16/go.mod h1:MHM4ouEsLNKXRYO+fS9qqpS1SFlL4Z6Q/0kxtS+auLk=
1111
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
1212
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
13+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
14+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1315
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
1416
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
1517
github.com/jessevdk/go-flags v1.6.1 h1:Cvu5U8UGrLay1rZfv/zP7iLpSHGUZ/Ou68T0iX1bBK4=

internals/template/core/core.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package core
22

3-
import "github.com/codeshelldev/goplater/internals/template/context"
3+
import (
4+
"github.com/codeshelldev/goplater/internals/template/context"
5+
)
46

57
var Renderer IRenderer
68
var Matcher IMatcher
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package funcs
2+
3+
import (
4+
"reflect"
5+
6+
"github.com/codeshelldev/goplater/internals/template/context"
7+
)
8+
9+
var containerDeleteFunc = TemplateFunc{
10+
Name: "delete",
11+
Handler: func(context context.TemplateContext, container any, key any) any {
12+
return deleteKey(container, key)
13+
},
14+
}
15+
16+
var containerSetFunc = TemplateFunc{
17+
Name: "set",
18+
Handler: func(context context.TemplateContext, container any, key any, value any) any {
19+
return setKey(container, key, value)
20+
},
21+
}
22+
23+
var slicePushFunc = TemplateFunc{
24+
Name: "push",
25+
Handler: func(context context.TemplateContext, container []any, value any) any {
26+
i := len(container) + 1
27+
28+
return setKey(container, i, value)
29+
},
30+
}
31+
32+
func deleteKey(data any, key any) any {
33+
val := reflect.ValueOf(data)
34+
if val.Kind() == reflect.Pointer {
35+
val = val.Elem()
36+
}
37+
38+
switch val.Kind() {
39+
case reflect.Map:
40+
for _, k := range val.MapKeys() {
41+
if reflect.DeepEqual(k.Interface(), key) {
42+
val.SetMapIndex(k, reflect.Value{})
43+
}
44+
}
45+
case reflect.Slice:
46+
newLen := 0
47+
48+
for i := 0; i < val.Len(); i++ {
49+
if !reflect.DeepEqual(val.Index(i).Interface(), key) {
50+
val.Index(newLen).Set(val.Index(i))
51+
52+
newLen++
53+
}
54+
}
55+
56+
val.Set(val.Slice(0, newLen))
57+
}
58+
59+
return data
60+
}
61+
62+
func setKey(data any, key any, value any) any {
63+
val := reflect.ValueOf(data)
64+
65+
if val.Kind() == reflect.Pointer {
66+
val = val.Elem()
67+
}
68+
69+
switch val.Kind() {
70+
case reflect.Map:
71+
val.SetMapIndex(reflect.ValueOf(key), reflect.ValueOf(value))
72+
case reflect.Slice:
73+
i, ok := key.(int)
74+
75+
if ok && i >= 0 {
76+
for val.Len() <= i {
77+
val.Set(reflect.Append(val, reflect.Zero(val.Type().Elem())))
78+
}
79+
80+
val.Index(i).Set(reflect.ValueOf(value))
81+
}
82+
}
83+
84+
return data
85+
}
86+
87+
func init() {
88+
Register(containerDeleteFunc)
89+
Register(containerSetFunc)
90+
91+
Register(slicePushFunc)
92+
}

internals/template/funcs/delete.go

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)