2023-04-02 11:11:09 +02:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-04-07 12:31:30 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2023-04-02 11:11:09 +02:00
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
2023-04-06 16:21:26 +02:00
|
|
|
|
2023-04-06 13:08:30 +02:00
|
|
|
// DefaultRenderTemplate is an adapter to implement the builtin golang text template renderer as resource.RenderTemplate.
|
2023-04-07 12:31:30 +02:00
|
|
|
func DefaultRenderTemplate(r Resource, sym string, values map[string]string, idx uint16, sizer *Sizer) (string, error) {
|
2023-04-06 16:21:26 +02:00
|
|
|
v, err := r.GetTemplate(sym, nil)
|
2023-04-02 11:11:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-04-07 12:31:30 +02:00
|
|
|
|
|
|
|
if sizer != nil {
|
|
|
|
values, err = sizer.GetAt(values, idx)
|
|
|
|
} else if idx > 0 {
|
|
|
|
return "", fmt.Errorf("sizer needed for indexed render")
|
|
|
|
}
|
|
|
|
log.Printf("render for index: %v", idx)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-04-02 11:11:09 +02:00
|
|
|
tp, err := template.New("tester").Option("missingkey=error").Parse(v)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := bytes.NewBuffer([]byte{})
|
|
|
|
err = tp.Execute(b, values)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return b.String(), err
|
|
|
|
}
|