| 1 |
package internal |
| 2 |
|
| 3 |
import ( |
| 4 |
"sync" |
| 5 |
|
| 6 |
"github.com/gohugoio/go-i18n/v2/i18n/template" |
| 7 |
) |
| 8 |
|
| 9 |
// Template stores the template for a string and a cached version of the parsed template if they are cacheable. |
| 10 |
type Template struct { |
| 11 |
Src string |
| 12 |
LeftDelim string |
| 13 |
RightDelim string |
| 14 |
|
| 15 |
parseOnce sync.Once |
| 16 |
parsedTemplate template.ParsedTemplate |
| 17 |
parseError error |
| 18 |
} |
| 19 |
|
| 20 |
func (t *Template) Execute(parser template.Parser, data interface{}) (string, error) { |
| 21 |
var pt template.ParsedTemplate |
| 22 |
var err error |
| 23 |
if parser.Cacheable() { |
| 24 |
t.parseOnce.Do(func() { |
| 25 |
t.parsedTemplate, t.parseError = parser.Parse(t.Src, t.LeftDelim, t.RightDelim) |
| 26 |
}) |
| 27 |
pt, err = t.parsedTemplate, t.parseError |
| 28 |
} else { |
| 29 |
pt, err = parser.Parse(t.Src, t.LeftDelim, t.RightDelim) |
| 30 |
} |
| 31 |
|
| 32 |
if err != nil { |
| 33 |
return "", err |
| 34 |
} |
| 35 |
return pt.Execute(data) |
| 36 |
} |