Funcs
とFuncMap
を使用しようとすると、Goテンプレートで奇妙なことに気付きました。期待通りに次のコードは動作します:Goテンプレート機能
buffer := bytes.NewBufferString("")
funcMap := template.FuncMap{
"label": strings.Title,
}
t, _ := template.New("alex").Funcs(funcMap).Parse("{{label \"alex\"}}")
t.Execute(buffer, "")
return string(buffer.Bytes()) //=> "Alex"
しかし、私は、ファイルにテンプレートを置くしようとすると、それは動作しません(Execute()
は言う:"alex" is an incomplete or empty template
):
t, _ := template.New("alex").Funcs(funcMap).ParseFiles("template.html")
template.htmlで:
{{label \"alex\"}}
理由は何ですか?これはバグですか?テンプレートでメソッド/関数を使用する簡単な方法はありますか?
まずアイデアを解析からのエラーをチェックし、実行することです。上のコードでは両方を無視します。 – Sonia
ええ、私はParseをチェックしましたが、Executeはチェックしていませんでした。 Executeは言う: "" alex "は不完全な、または空のテンプレート" – Blacksad