aboutsummaryrefslogtreecommitdiff
path: root/src/html/template/template.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/html/template/template.go')
-rw-r--r--src/html/template/template.go58
1 files changed, 52 insertions, 6 deletions
diff --git a/src/html/template/template.go b/src/html/template/template.go
index 75437879e2..bc960afe5f 100644
--- a/src/html/template/template.go
+++ b/src/html/template/template.go
@@ -7,7 +7,9 @@ package template
import (
"fmt"
"io"
+ "io/fs"
"io/ioutil"
+ "path"
"path/filepath"
"sync"
"text/template"
@@ -384,7 +386,7 @@ func Must(t *Template, err error) *Template {
// For instance, ParseFiles("a/foo", "b/foo") stores "b/foo" as the template
// named "foo", while "a/foo" is unavailable.
func ParseFiles(filenames ...string) (*Template, error) {
- return parseFiles(nil, filenames...)
+ return parseFiles(nil, readFileOS, filenames...)
}
// ParseFiles parses the named files and associates the resulting templates with
@@ -396,12 +398,12 @@ func ParseFiles(filenames ...string) (*Template, error) {
//
// ParseFiles returns an error if t or any associated template has already been executed.
func (t *Template) ParseFiles(filenames ...string) (*Template, error) {
- return parseFiles(t, filenames...)
+ return parseFiles(t, readFileOS, filenames...)
}
// parseFiles is the helper for the method and function. If the argument
// template is nil, it is created from the first file.
-func parseFiles(t *Template, filenames ...string) (*Template, error) {
+func parseFiles(t *Template, readFile func(string) (string, []byte, error), filenames ...string) (*Template, error) {
if err := t.checkCanParse(); err != nil {
return nil, err
}
@@ -411,12 +413,11 @@ func parseFiles(t *Template, filenames ...string) (*Template, error) {
return nil, fmt.Errorf("html/template: no files named in call to ParseFiles")
}
for _, filename := range filenames {
- b, err := ioutil.ReadFile(filename)
+ name, b, err := readFile(filename)
if err != nil {
return nil, err
}
s := string(b)
- name := filepath.Base(filename)
// First template becomes return value if not already defined,
// and we use that one for subsequent New calls to associate
// all the templates together. Also, if this file has the same name
@@ -479,7 +480,7 @@ func parseGlob(t *Template, pattern string) (*Template, error) {
if len(filenames) == 0 {
return nil, fmt.Errorf("html/template: pattern matches no files: %#q", pattern)
}
- return parseFiles(t, filenames...)
+ return parseFiles(t, readFileOS, filenames...)
}
// IsTrue reports whether the value is 'true', in the sense of not the zero of its type,
@@ -488,3 +489,48 @@ func parseGlob(t *Template, pattern string) (*Template, error) {
func IsTrue(val interface{}) (truth, ok bool) {
return template.IsTrue(val)
}
+
+// ParseFS is like ParseFiles or ParseGlob but reads from the file system fs
+// instead of the host operating system's file system.
+// It accepts a list of glob patterns.
+// (Note that most file names serve as glob patterns matching only themselves.)
+func ParseFS(fs fs.FS, patterns ...string) (*Template, error) {
+ return parseFS(nil, fs, patterns)
+}
+
+// ParseFS is like ParseFiles or ParseGlob but reads from the file system fs
+// instead of the host operating system's file system.
+// It accepts a list of glob patterns.
+// (Note that most file names serve as glob patterns matching only themselves.)
+func (t *Template) ParseFS(fs fs.FS, patterns ...string) (*Template, error) {
+ return parseFS(t, fs, patterns)
+}
+
+func parseFS(t *Template, fsys fs.FS, patterns []string) (*Template, error) {
+ var filenames []string
+ for _, pattern := range patterns {
+ list, err := fs.Glob(fsys, pattern)
+ if err != nil {
+ return nil, err
+ }
+ if len(list) == 0 {
+ return nil, fmt.Errorf("template: pattern matches no files: %#q", pattern)
+ }
+ filenames = append(filenames, list...)
+ }
+ return parseFiles(t, readFileFS(fsys), filenames...)
+}
+
+func readFileOS(file string) (name string, b []byte, err error) {
+ name = filepath.Base(file)
+ b, err = ioutil.ReadFile(file)
+ return
+}
+
+func readFileFS(fsys fs.FS) func(string) (string, []byte, error) {
+ return func(file string) (name string, b []byte, err error) {
+ name = path.Base(file)
+ b, err = fs.ReadFile(fsys, file)
+ return
+ }
+}