Source File
helper.go
Belonging Package
text/template
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Helper functions to make constructing templates easier.
package template
import (
)
// Functions and methods to parse templates.
// Must is a helper that wraps a call to a function returning ([*Template], error)
// and panics if the error is non-nil. It is intended for use in variable
// initializations such as
//
// var t = template.Must(template.New("name").Parse("text"))
func ( *Template, error) *Template {
if != nil {
panic()
}
return
}
// ParseFiles creates a new [Template] and parses the template definitions from
// the named files. The returned template's name will have the base name and
// parsed contents of the first file. There must be at least one file.
// If an error occurs, parsing stops and the returned *Template is nil.
//
// When parsing multiple files with the same name in different directories,
// the last one mentioned will be the one that results.
// For instance, ParseFiles("a/foo", "b/foo") stores "b/foo" as the template
// named "foo", while "a/foo" is unavailable.
func ( ...string) (*Template, error) {
return parseFiles(nil, readFileOS, ...)
}
// ParseFiles parses the named files and associates the resulting templates with
// t. If an error occurs, parsing stops and the returned template is nil;
// otherwise it is t. There must be at least one file.
// Since the templates created by ParseFiles are named by the base
// (see [filepath.Base]) names of the argument files, t should usually have the
// name of one of the (base) names of the files. If it does not, depending on
// t's contents before calling ParseFiles, t.Execute may fail. In that
// case use t.ExecuteTemplate to execute a valid template.
//
// When parsing multiple files with the same name in different directories,
// the last one mentioned will be the one that results.
func ( *Template) ( ...string) (*Template, error) {
.init()
return parseFiles(, readFileOS, ...)
}
// parseFiles is the helper for the method and function. If the argument
// template is nil, it is created from the first file.
func parseFiles( *Template, func(string) (string, []byte, error), ...string) (*Template, error) {
if len() == 0 {
// Not really a problem, but be consistent.
return nil, fmt.Errorf("template: no files named in call to ParseFiles")
}
for , := range {
, , := ()
if != nil {
return nil,
}
:= string()
// 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
// as t, this file becomes the contents of t, so
// t, err := New(name).Funcs(xxx).ParseFiles(name)
// works. Otherwise we create a new template associated with t.
var *Template
if == nil {
= New()
}
if == .Name() {
=
} else {
= .New()
}
_, = .Parse()
if != nil {
return nil,
}
}
return , nil
}
// ParseGlob creates a new [Template] and parses the template definitions from
// the files identified by the pattern. The files are matched according to the
// semantics of [filepath.Match], and the pattern must match at least one file.
// The returned template will have the [filepath.Base] name and (parsed)
// contents of the first file matched by the pattern. ParseGlob is equivalent to
// calling [ParseFiles] with the list of files matched by the pattern.
//
// When parsing multiple files with the same name in different directories,
// the last one mentioned will be the one that results.
func ( string) (*Template, error) {
return parseGlob(nil, )
}
// ParseGlob parses the template definitions in the files identified by the
// pattern and associates the resulting templates with t. The files are matched
// according to the semantics of [filepath.Match], and the pattern must match at
// least one file. ParseGlob is equivalent to calling [Template.ParseFiles] with
// the list of files matched by the pattern.
//
// When parsing multiple files with the same name in different directories,
// the last one mentioned will be the one that results.
func ( *Template) ( string) (*Template, error) {
.init()
return parseGlob(, )
}
// parseGlob is the implementation of the function and method ParseGlob.
func parseGlob( *Template, string) (*Template, error) {
, := filepath.Glob()
if != nil {
return nil,
}
if len() == 0 {
return nil, fmt.Errorf("template: pattern matches no files: %#q", )
}
return parseFiles(, readFileOS, ...)
}
// ParseFS is like [Template.ParseFiles] or [Template.ParseGlob] but reads from the file system fsys
// instead of the host operating system's file system.
// It accepts a list of glob patterns (see [path.Match]).
// (Note that most file names serve as glob patterns matching only themselves.)
func ( fs.FS, ...string) (*Template, error) {
return parseFS(nil, , )
}
// ParseFS is like [Template.ParseFiles] or [Template.ParseGlob] but reads from the file system fsys
// instead of the host operating system's file system.
// It accepts a list of glob patterns (see [path.Match]).
// (Note that most file names serve as glob patterns matching only themselves.)
func ( *Template) ( fs.FS, ...string) (*Template, error) {
.init()
return parseFS(, , )
}
func parseFS( *Template, fs.FS, []string) (*Template, error) {
var []string
for , := range {
, := fs.Glob(, )
if != nil {
return nil,
}
if len() == 0 {
return nil, fmt.Errorf("template: pattern matches no files: %#q", )
}
= append(, ...)
}
return parseFiles(, readFileFS(), ...)
}
func readFileOS( string) ( string, []byte, error) {
= filepath.Base()
, = os.ReadFile()
return
}
func readFileFS( fs.FS) func(string) (string, []byte, error) {
return func( string) ( string, []byte, error) {
= path.Base()
, = fs.ReadFile(, )
return
}
}
The pages are generated with Golds v0.7.0-preview. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |