You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
543 B
Go
29 lines
543 B
Go
3 years ago
|
package tmpl
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"io"
|
||
|
)
|
||
|
|
||
|
type Executor interface {
|
||
|
Execute(wr io.Writer, data interface{}) error
|
||
|
}
|
||
|
|
||
|
// AsString to convert template to string
|
||
|
func AsString(t Executor, data interface{}) (string, error) {
|
||
|
var buf bytes.Buffer
|
||
|
if err := t.Execute(&buf, data); err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
return buf.String(), nil
|
||
|
}
|
||
|
|
||
|
// String to convert template to string and ignores errors
|
||
|
func String(t Executor, data interface{}) string {
|
||
|
if result, err := AsString(t, data); err == nil {
|
||
|
return result
|
||
|
} else {
|
||
|
return ""
|
||
|
}
|
||
|
}
|