Skip to content

Commit

Permalink
feat: add context template func to pass new dict to partials
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjarosch committed Feb 9, 2024
1 parent 9d18bda commit 7728327
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
15 changes: 10 additions & 5 deletions examples/partial_templates/compiled/example/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@ this partial does not use any data

---

{network:
foo: bar
skipper:
use:
- network
example}

---



# example

```
network:
foo: bar
skipper:
use:
- network
foo: bar
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# {{ .TargetName }}

```
{{ .Inventory }}
{{ .Network }}
```

{{ end }}
6 changes: 5 additions & 1 deletion examples/partial_templates/templates/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@

---

{{ template "with_data" . }}
{{ . }}

---

{{ template "with_data" context "TargetName" .TargetName "Network" .Inventory.network }}
16 changes: 16 additions & 0 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ var customFuncs map[string]any = map[string]any{
}
return time.Now().AddDate(y, m, d).Format(time.RFC3339)
},

"context": func(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, fmt.Errorf("uneven amount of values")
}
context := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, fmt.Errorf("map key must be a string")
}
context[key] = values[i+1]
}

return context, nil
},
}

type Templater struct {
Expand Down

0 comments on commit 7728327

Please sign in to comment.