Skip to content

Instantly share code, notes, and snippets.

@kwmt
Created March 18, 2013 12:40
Show Gist options
  • Save kwmt/5186898 to your computer and use it in GitHub Desktop.
Save kwmt/5186898 to your computer and use it in GitHub Desktop.
dir := createTestDir([]templateFile{
// T0.tmpl はT1をコールします。
{"T0.tmpl", `T0 invokes T1: ({{template "T1"}})`},
// T1.tmpl T2をコールするテンプレートを"T1"として定義しています。
{"T1.tmpl", `{{define "T1"}}T1 invokes T2: ({{template "T2"}}){{end}}`},
// T2.tmpl は"T2"としてテンプレートを定義しています。
{"T2.tmpl", `{{define "T2"}}This is T2{{end}}`},
})
// main関数終了後、作成したディレクトリを削除します。
defer os.RemoveAll(dir)
// dirにあるすべてのテンプレートファイルを検索するようにパターンを作成しています。
pattern := filepath.Join(dir, "*.tmpl")
// ここから本題です。
// T0.tmpl は初めにマッチする名前ですので、始めのテンプレート(ParseGlobの戻り値の値)になります。
tmpl := template.Must(template.ParseGlob(pattern))
log.Println(tmpl.Name())
err := tmpl.Execute(os.Stdout, nil)
if err != nil {
log.Fatalf("template execution: %s", err)
}
// Output:
// T0 invokes T1: (T1 invokes T2: (This is T2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment