Documentation ¶
Overview ¶
Package recurparse parsing go templates recursively, instead of default template behavior that puts all files together.
It goes through subfolders recursively and parses the files matching the glob. The templates have the subfolder path in the name, separated by forward slash (even on windows).
The template names are as relative to the given folder.
All the 4 functions behave in similar way.
If the first argument is nil, the resulting template will have one of the files as name and content; if it's an existing template, it will add the files as associated templates.
The pattern works only on the final filename; that is, k*.html will match foo/bar/kxxx.html; it does NOT filter the directory name, all directories are walked through.
The matching logic is using filepath.Match on the filename, in the same way template.Parse do it. It follows all symlinks, the symlinks will be there under the symlink name. If there is a "symlink loop" (that is, symlink to .. or similar), the function will panic and run out of memory.
If there is no files that matches, the function errors, same as go's ParseFiles.
Index ¶
- func HTMLParse(t *templateHtml.Template, dirPath, glob string) (*templateHtml.Template, error)
- func HTMLParseFS(t *templateHtml.Template, fsys fs.FS, glob string) (*templateHtml.Template, error)
- func TextParse(t *templateText.Template, dirPath, glob string) (*templateText.Template, error)
- func TextParseFS(t *templateText.Template, fsys fs.FS, glob string) (*templateText.Template, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HTMLParse ¶
func HTMLParse(t *templateHtml.Template, dirPath, glob string) (*templateHtml.Template, error)
HTMLParse opens a fs.FS filesystem and recursively parses the files there as HTML templates.
See package docs for details of the behavior.
func HTMLParseFS ¶
func HTMLParseFS(t *templateHtml.Template, fsys fs.FS, glob string) (*templateHtml.Template, error)
HTMLParseFS opens a fs.FS filesystem and recursively parses the files there as HTML templates.
See package docs for details of the behavior.
Example ¶
fsTest := fstest.MapFS{ "data.txt": &fstest.MapFile{ Data: []byte("super simple {{.Foo}}"), }, "some/deep/file.txt": &fstest.MapFile{ Data: []byte("other simple {{.Foo}}"), }, } tmpl, err := HTMLParseFS( nil, fsTest, "*.txt", ) if err != nil { panic(err) } err = tmpl.ExecuteTemplate(os.Stdout, "some/deep/file.txt", struct{ Foo string }{Foo: "bar"}) if err != nil { panic(err) }
Output: other simple bar
func TextParse ¶
func TextParse(t *templateText.Template, dirPath, glob string) (*templateText.Template, error)
TextParse opens a directory and recursively parses the files there as text templates.
See package docs for details of the behavior.
func TextParseFS ¶
func TextParseFS(t *templateText.Template, fsys fs.FS, glob string) (*templateText.Template, error)
TextParseFS opens a fs.FS filesystem and recursively parses the files there as text templates.
See package docs for details of the behavior.
Types ¶
This section is empty.