目录 搜索
archive archive/tar archive/zip bufio bufio(缓存) builtin builtin(内置包) bytes bytes(包字节) compress compress/bzip2(压缩/bzip2) compress/flate(压缩/flate) compress/gzip(压缩/gzip) compress/lzw(压缩/lzw) compress/zlib(压缩/zlib) container container/heap(容器数据结构heap) container/list(容器数据结构list) container/ring(容器数据结构ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(数据库/sql) database/sql/driver(数据库/sql/driver) debug debug/dwarf(调试/dwarf) debug/elf(调试/elf) debug/gosym(调试/gosym) debug/macho(调试/macho) debug/pe(调试/pe) debug/plan9obj(调试/plan9obj) encoding encoding(编码) encoding/ascii85(编码/ascii85) encoding/asn1(编码/asn1) encoding/base32(编码/base32) encoding/base64(编码/base64) encoding/binary(编码/binary) encoding/csv(编码/csv) encoding/gob(编码/gob) encoding/hex(编码/hex) encoding/json(编码/json) encoding/pem(编码/pem) encoding/xml(编码/xml) errors errors(错误) expvar expvar flag flag(命令行参数解析flag包) fmt fmt go go/ast(抽象语法树) go/build go/constant(常量) go/doc(文档) go/format(格式) go/importer go/parser go/printer go/scanner(扫描仪) go/token(令牌) go/types(类型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(图像) image/color(颜色) image/color/palette(调色板) image/draw(绘图) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系统) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路径) plugin plugin(插件) reflect reflect(反射) regexp regexp(正则表达式) regexp/syntax runtime runtime(运行时) runtime/debug(调试) runtime/internal/sys runtime/pprof runtime/race(竞争) runtime/trace(执行追踪器) sort sort(排序算法) strconv strconv(转换) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系统调用) testing testing(测试) testing/iotest testing/quick text text/scanner(扫描文本) text/tabwriter text/template(定义模板) text/template/parse time time(时间戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
文字

  • import "text/template"

  • 概观

  • 索引

  • Examples

  • 子目录

概观

打包模板实现了用于生成文本输出的数据驱动模板。

要生成 HTML 输出,请参阅包 html / template ,它与此包具有相同的接口,但会自动保护 HTML 输出免受某些攻击。

通过将模板应用于数据结构来执行模板。模板中的注释引用数据结构的元素(通常是结构中的字段或映射中的键)来控制执行并导出要显示的值。模板的执行遍历结构并设置光标,以句点'。'表示。并称为“点”,作为执行过程中结构中当前位置的值。

模板的输入文本是任何格式的 UTF-8 编码文本。“行动” - 数据评估或控制结构 - 由“{{”和“}}”分隔。所有文本以外的动作都将被复制到输出中。除了原始字符串,操作可能不会跨越换行符,尽管注释可以。

一旦解析,模板可以并行安全地执行,但如果并行执行共享 Writer,则输出可能会交错。

这是一个简单的例子,打印出“17件羊毛制品”。

type Inventory struct {
	Material string
	Count    uint}sweaters := Inventory{"wool", 17}tmpl, err := template.New("test").Parse("{{.Count}} items are made of {{.Material}}")if err != nil { panic(err) }err = tmpl.Execute(os.Stdout, sweaters)if err != nil { panic(err) }

下面显示更复杂的例子。

文本和空格

默认情况下,在执行模板时,动作之间的所有文本都会逐字复制。例如,在程序运行时,上述示例中的字符串“由...构成”出现在标准输出中。

但是,为了帮助格式化模板源代码,如果动作的左分隔符(默认为“{{”)后面紧跟着一个减号和 ASCII 空格字符(“{{ - ”)),则所有尾随空白符将从紧接在文本之前。同样,如果右分隔符(“}}”)前面有空格和减号(“ - }}”),则所有前导空格都将从紧随其后的文本中删除。在这些修剪标记中,ASCII空间必须存在; “{{-3}}”解析为包含数字-3的操作。

例如,当执行源代码为的模板时

"{{23 -}} < {{- 45}}"

生成的输出将是

"23<45"

对于这种修剪,空白字符的定义与 Go :空格,水平制表符,回车符和换行符相同。

操作

这是行动列表。“参数”和“管道”是对数据的评估,在后面的相应部分详细说明。

{{/* a comment */}}
	A comment; discarded. May contain newlines.
	Comments do not nest and must start and end at the
	delimiters, as shown here.{{pipeline}}
	The default textual representation (the same as would be
	printed by fmt.Print) of the value of the pipeline is copied
	to the output.{{if pipeline}} T1 {{end}}
	If the value of the pipeline is empty, no output is generated;
	otherwise, T1 is executed. The empty values are false, 0, any
	nil pointer or interface value, and any array, slice, map, or
	string of length zero.
	Dot is unaffected.{{if pipeline}} T1 {{else}} T0 {{end}}
	If the value of the pipeline is empty, T0 is executed;
	otherwise, T1 is executed. Dot is unaffected.{{if pipeline}} T1 {{else if pipeline}} T0 {{end}}
	To simplify the appearance of if-else chains, the else actionof an if may include another if directly; the effect is exactly
	the same as writing{{if pipeline}} T1 {{else}}{{if pipeline}} T0 {{end}}{{end}}{{range pipeline}} T1 {{end}}
	The value of the pipeline must be an array, slice, map, or channel.
	If the value of the pipeline has length zero, nothing is output;
	otherwise, dot is set to the successive elements of the array,
	slice, or map and T1 is executed. If the value is a map and the
	keys are of basic type with a defined order ("comparable"), the
	elements will be visited in sorted key order.{{range pipeline}} T1 {{else}} T0 {{end}}
	The value of the pipeline must be an array, slice, map, or channel.
	If the value of the pipeline has length zero, dot is unaffected and
	T0 is executed; otherwise, dot is set to the successive elementsof the array, slice, or map and T1 is executed.{{template "name"}}
	The template with the specified name is executed with nil data.{{template "name" pipeline}}
	The template with the specified name is executed with dot set
	to the value of the pipeline.{{block "name" pipeline}} T1 {{end}}
	A block is shorthand for defining a template{{define "name"}} T1 {{end}}
	and then executing it in place{{template "name" .}}
	The typical use is to define a set of root templates that are
	then customized by redefining the block templates within.{{with pipeline}} T1 {{end}}
	If the value of the pipeline is empty, no output is generated;
	otherwise, dot is set to the value of the pipeline and T1 is
	executed.{{with pipeline}} T1 {{else}} T0 {{end}}
	If the value of the pipeline is empty, dot is unaffected and T0
	is executed; otherwise, dot is set to the value of the pipeline
	and T1 is executed.

参数

参数是一个简单的值,表示为以下之一。

- A boolean, string, character, integer, floating-point, imaginary
  or complex constant in Go syntax. These behave like Go's untyped
  constants.- The keyword nil, representing an untyped Go nil.- The character '.' (period):.
  The result is the value of dot.- A variable name, which is a (possibly empty) alphanumeric string
  preceded by a dollar sign, such as
	$piOver2
  or
	$
  The result is the value of the variable.
  Variables are described below.- The name of a field of the data, which must be a struct, preceded
  by a period, such as.Field
  The result is the value of the field. Field invocations may be
  chained:    .Field1.Field2
  Fields can also be evaluated on variables, including chaining:
    $x.Field1.Field2- The name of a key of the data, which must be a map, preceded
  by a period, such as.Key
  The result is the map element value indexed by the key.
  Key invocations may be chained and combined with fields to any
  depth:    .Field1.Key1.Field2.Key2
  Although the key must be an alphanumeric identifier, unlike with
  field names they do not need to start with an upper case letter.
  Keys can also be evaluated on variables, including chaining:
    $x.key1.key2- The name of a niladic method of the data, preceded by a period,
  such as.Method
  The result is the value of invoking the method with dot as the
  receiver, dot.Method(). Such a method must have one return value (of
  any type) or two return values, the second of which is an error.
  If it has two and the returned error is non-nil, execution terminates
  and an error is returned to the caller as the value of Execute.
  Method invocations may be chained and combined with fields and keys
  to any depth:    .Field1.Key1.Method1.Field2.Key2.Method2
  Methods can also be evaluated on variables, including chaining:
    $x.Method1.Field- The name of a niladic function, such as
	fun
  The result is the value of invoking the function, fun(). The return
  types and values behave as in methods. Functions and function
  names are described below.- A parenthesized instance of one the above, for grouping. The result
  may be accessed by a field or map key invocation.print (.F1 arg1) (.F2 arg2)(.StructValuedMethod "arg").Field

参数可以评估为任何类型;如果它们是指针,则实现会在需要时自动指向基本类型。如果一个评估产生一个函数值,比如一个 struct 的函数值域,那么该函数不会被自动调用,但它可以用作 if 动作等的真值。要调用它,请使用下面定义的调用函数。

管道

流水线是可能链接的“命令”序列。命令是一个简单的值(参数)或函数或方法调用,可能有多个参数:

Argument
	The result is the value of evaluating the argument..Method [Argument...]
	The method can be alone or the last element of a chain but,
	unlike methods in the middle of a chain, it can take arguments.
	The result is the value of calling the method with the
	arguments:
		dot.Method(Argument1, etc.)functionName [Argument...]
	The result is the value of calling the function associatedwith the name:function(Argument1, etc.)
	Functions and function names are described below.

管道可以通过用流水线字符 '|' 分隔一系列命令来“链接”。在链式管道中,每个命令的结果都作为以下命令的最后一个参数传递。管道中最终命令的输出是管道的值。

命令的输出可以是一个值或两个值,其中第二个具有类型错误。如果第二个值存在并且评估为非零,则执行终止,并将错误返回给执行的调用者。

变量

动作中的管道可以初始化一个变量来捕获结果。初始化有语法

$variable := pipeline

其中 $ variable 是变量的名称。声明变量的动作不会产生输出。

如果“范围”操作初始化一个变量,则该变量将设置为迭代的连续元素。此外,“范围”可以声明两个变量,用逗号分隔:

range $index, $element := pipeline

在这种情况下,$ index 和 $ element 分别被设置为数组/索引或映射键和元素的连续值。请注意,如果只有一个变量,则会分配该元素; 这与 Go 范围内的约定相反。

变量的作用域扩展到声明它的控制结构(“if”,“with”或“range”)的“end”动作,或者如果没有这样的控制结构,则扩展到模板的末尾。模板调用不会从其调用点继承变量。

当执行开始时,$ 被设置为传递给 Execute 的数据参数,也就是点的起始值。

示例

下面是一些演示流水线和变量的单行模板示例。所有产生引用的单词“输出”:

{{"\"output\""}}
	A string constant.{{`"output"`}}
	A raw string constant.{{printf "%q" "output"}}
	A function call.{{"output" | printf "%q"}}
	A function call whose final argument comes from the previous
	command.{{printf "%q" (print "out" "put")}}
	A parenthesized argument.{{"put" | printf "%s%s" "out" | printf "%q"}}
	A more elaborate call.{{"output" | printf "%s" | printf "%q"}}
	A longer chain.{{with "output"}}{{printf "%q" .}}{{end}}
	A with action using dot.{{with $x := "output" | printf "%q"}}{{$x}}{{end}}
	A with action that creates and uses a variable.{{with $x := "output"}}{{printf "%q" $x}}{{end}}
	A with action that uses the variable in another action.{{with $x := "output"}}{{$x | printf "%q"}}{{end}}
	The same, but pipelined.

功能

在执行期间,函数可以在两个函数图中找到:首先在模板中,然后在全局函数图中找到。默认情况下,模板中没有定义函数,但可以使用 Funcs 方法添加它们。

预定义的全局函数名称如下。

and
	Returns the boolean AND of its arguments by returning the
	first empty argument or the last argument, that is,"and x y" behaves as "if x then y else x". All the
	arguments are evaluated.call
	Returns the result of calling the first argument, which
	must be a function, with the remaining arguments as parameters.
	Thus "call .X.Y 1 2" is, in Go notation, dot.X.Y(1, 2) where
	Y is a func-valued field, map entry, or the like.
	The first argument must be the result of an evaluation
	that yields a value of function type (as distinct from
	a predefined function such as print). The function mustreturn either one or two result values, the second of which
	is of type error. If the arguments don't match the function
	or the returned error value is non-nil, execution stops.html
	Returns the escaped HTML equivalent of the textual
	representation of its arguments. This function is unavailablein html/template, with a few exceptions.index
	Returns the result of indexing its first argument by the
	following arguments. Thus "index x 1 2 3" is, in Go syntax,
	x[1][2][3]. Each indexed item must be a map, slice, or array.js
	Returns the escaped JavaScript equivalent of the textual
	representation of its arguments.len
	Returns the integer length of its argument.not
	Returns the boolean negation of its single argument.or
	Returns the boolean OR of its arguments by returning the
	first non-empty argument or the last argument, that is,"or x y" behaves as "if x then x else y". All the
	arguments are evaluated.print
	An alias for fmt.Sprint
printf
	An alias for fmt.Sprintf
println
	An alias for fmt.Sprintln
urlquery
	Returns the escaped value of the textual representation of
	its arguments in a form suitable for embedding in a URL query.
	This function is unavailable in html/template, with a few
	exceptions.

布尔函数将任何零值设为 false,并将非零值设为 true。

还有一组定义为函数的二进制比较运算符:

eq
	Returns the boolean truth of arg1 == arg2
ne
	Returns the boolean truth of arg1 != arg2
lt
	Returns the boolean truth of arg1 < arg2
le
	Returns the boolean truth of arg1 <= arg2
gt
	Returns the boolean truth of arg1 > arg2
ge
	Returns the boolean truth of arg1 >= arg2

对于更简单的多路平等测试,eq(only)接受两个或更多个参数,并比较第一个和后面的第一个,返回有效

arg1==arg2 || arg1==arg3 || arg1==arg4 ...

(与 Go 中的||不同,然而,eq 是一个函数调用,所有参数都将被评估。)

比较函数仅适用于基本类型(或指定的基本类型,例如“类型 Celsius float32”)。它们实现 Go 规则以比较值,但忽略大小和确切类型,因此可以将任何有符号或无符号整数值与任何其他整数值进行比较。(算术值被比较,而不是比特模式,因此所有负整数都小于所有无符号整数。)但是,像往常一样,不能将 int 与 float32 等进行比较。

关联的模板

每个模板都由创建时指定的字符串命名。而且,每个模板都与零个或多个其他模板相关联,可以按名称调用它们;这种关联是可传递的,并形成模板的名称空间。

模板可以使用模板调用来实例化另一个相关联的模板;请参阅上面“模板”操作的解释。该名称必须是与包含该调用的模板关联的模板的名称。

嵌套的模板定义

解析模板时,可能会定义另一个模板并将其与正在解析的模板关联。模板定义必须出现在模板的顶层,非常像 Go 程序中的全局变量。

这些定义的语法是用“定义”和“结束”动作包围每个模板声明。

定义操作通过提供字符串常量来命名正在创建的模板。这是一个简单的例子:

`{{define "T1"}}ONE{{end}}
{{define "T2"}}TWO{{end}}
{{define "T3"}}{{template "T1"}} {{template "T2"}}{{end}}
{{template "T3"}}`

这定义了两个模板 T1 和 T2,以及第三个 T3 在执行时调用其他两个模板。最后它调用 T3。如果执行此模板将生成文本

ONE TWO

通过构建,模板可以仅存在于一个关联中。如果必须有可从多个关联中寻址的模板,则必须多次解析模板定义以创建不同的 * Template 值,或者必须使用 Clone 或 AddParseTree 方法复制该模板定义。

可以多次调用解析来组装各种关联的模板;请参阅 ParseFiles 和 ParseGlob 函数和方法,以获取解析存储在文件中的相关模板的简单方法。

模板可以直接执行或通过 ExecuteTemplate 执行,后者执行由名称标识的相关模板。为了调用我们上面的例子,我们可以写,

err := tmpl.Execute(os.Stdout, "no data needed")if err != nil {
	log.Fatalf("execution failed: %s", err)}

或通过名称显式调用特定的模板,

err := tmpl.ExecuteTemplate(os.Stdout, "T2", "no data needed")if err != nil {
	log.Fatalf("execution failed: %s", err)}

索引

  • func HTMLEscape(w io.Writer, b []byte)

  • func HTMLEscapeString(s string) string

  • func HTMLEscaper(args ...interface{}) string

  • func IsTrue(val interface{}) (truth, ok bool)

  • func JSEscape(w io.Writer, b []byte)

  • func JSEscapeString(s string) string

  • func JSEscaper(args ...interface{}) string

  • func URLQueryEscaper(args ...interface{}) string

  • type ExecError

  • func (e ExecError) Error() string

  • type FuncMap

  • type Template

  • func Must(t *Template, err error) *Template

  • func New(name string) *Template

  • func ParseFiles(filenames ...string) (*Template, error)

  • func ParseGlob(pattern string) (*Template, error)

  • func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error)

  • func (t *Template) Clone() (*Template, error)

  • func (t *Template) DefinedTemplates() string

  • func (t *Template) Delims(left, right string) *Template

  • func (t *Template) Execute(wr io.Writer, data interface{}) error

  • func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) error

  • func (t *Template) Funcs(funcMap FuncMap) *Template

  • func (t *Template) Lookup(name string) *Template

  • func (t *Template) Name() string

  • func (t *Template) New(name string) *Template

  • func (t *Template) Option(opt ...string) *Template

  • func (t *Template) Parse(text string) (*Template, error)

  • func (t *Template) ParseFiles(filenames ...string) (*Template, error)

  • func (t *Template) ParseGlob(pattern string) (*Template, error)

  • func (t *Template) Templates() []*Template

示例

Template Template (Block) Template (Func) Template (Glob) Template (Helpers) Template (Share)

打包文件

doc.go exec.go funcs.go helper.go option.go template.go

func HTMLEscape

func HTMLEscape(w io.Writer, b []byte)

HTMLEscape 写入到明文数据 b 的转义 HTML 等价物中。

func HTMLEscapeString

func HTMLEscapeString(s string) string

HTMLEscapeString 返回纯文本数据的转义 HTML 等价物。

func HTMLEscaper

func HTMLEscaper(args ...interface{}) string

HTMLEscaper 返回其参数文本表示的转义 HTML 等价物。

func IsTrue

func IsTrue(val interface{}) (truth, ok bool)

IsTrue 报告该值是否为'真',意味着它的类型不为零,以及该值是否具有有意义的真值。这是if和其他此类行为所使用的真相的定义。

func JSEscape

func JSEscape(w io.Writer, b []byte)

JSEscape 写入 w 的纯文本数据 b 的逃逸 JavaScript 等价物。

func JSEscapeString

func JSEscapeString(s string) string

JSEscapeString 返回纯文本数据的转义 JavaScript 等价物。

func JSEscaper

func JSEscaper(args ...interface{}) string

JSEscaper 返回其参数的文本表示的转义 JavaScript 等价物。

func URLQueryEscaper

func URLQueryEscaper(args ...interface{}) string

URLQueryEscaper 以适合于嵌入到 URL 查询中的形式返回其参数的文本表示的转义值。

type ExecError

ExecError 是 Execute 在评估其模板时发生错误时返回的自定义错误类型。(如果发生写入错误,则返回实际错误;它不会是 ExecError 类型。)

type ExecError struct {
        Name string // Name of template.
        Err  error  // Pre-formatted error.}

func (ExecError) Error

func (e ExecError) Error() string

键入FuncMap

FuncMap 是定义从名称到函数映射的映射的类型。每个函数必须具有单个返回值,或者其中第二个具有类型错误的返回值。在这种情况下,如果第二个(错误)返回值在执行过程中评估为非零,则执行结束,并执行返回该错误。

当模板执行使用参数列表调用函数时,该列表必须可分配给函数的参数类型。旨在应用于任意类型参数的函数可以使用类型为 interface {} 或类型为 reflect.Value 的参数。同样,函数意味着返回任意类型的结果,可以返回 interface {} 或 reflect.Value。

type FuncMap map[string]interface{}

键入Template

Template 是分析模板的表示。* parse.Tree字段仅导出供 html / template 使用,并应被视为未被所有其他客户端导出。

type Template struct {        *parse.Tree        // contains filtered or unexported fields}

示例

package mainimport ("log""os""text/template")func main() {// Define a template.const letter = `
Dear {{.Name}},
{{if .Attended}}
It was a pleasure to see you at the wedding.
{{- else}}
It is a shame you couldn't make it to the wedding.
{{- end}}
{{with .Gift -}}
Thank you for the lovely {{.}}.
{{end}}
Best wishes,
Josie
`// Prepare some data to insert into the template.
	type Recipient struct {
		Name, Gift string
		Attended   bool}var recipients = []Recipient{{"Aunt Mildred", "bone china tea set", true},{"Uncle John", "moleskin pants", false},{"Cousin Rodney", "", false},}// Create a new template and parse the letter into it.
	t := template.Must(template.New("letter").Parse(letter))// Execute the template for each recipient.for _, r := range recipients {
		err := t.Execute(os.Stdout, r)if err != nil {
			log.Println("executing template:", err)}}}

示例(块)

package mainimport ("log""os""strings""text/template")func main() {const (
		master  = `Names:{{block "list" .}}{{"\n"}}{{range .}}{{println "-" .}}{{end}}{{end}}`
		overlay = `{{define "list"}} {{join . ", "}}{{end}} `)var (
		funcs     = template.FuncMap{"join": strings.Join}
		guardians = []string{"Gamora", "Groot", "Nebula", "Rocket", "Star-Lord"})
	masterTmpl, err := template.New("master").Funcs(funcs).Parse(master)if err != nil {
		log.Fatal(err)}
	overlayTmpl, err := template.Must(masterTmpl.Clone()).Parse(overlay)if err != nil {
		log.Fatal(err)}if err := masterTmpl.Execute(os.Stdout, guardians); err != nil {
		log.Fatal(err)}if err := overlayTmpl.Execute(os.Stdout, guardians); err != nil {
		log.Fatal(err)}}

示例(Func)

此示例演示了处理模板文本的自定义函数。它会安装 strings.Title 函数并使用它在我们的模板输出中使标题文字看起来很好。

package mainimport ("log""os""strings""text/template")func main() {// First we create a FuncMap with which to register the function.
	funcMap := template.FuncMap{// The name "title" is what the function will be called in the template text."title": strings.Title,}// A simple template definition to test our function.// We print the input text several ways:// - the original// - title-cased// - title-cased and then printed with %q// - printed with %q and then title-cased.const templateText = `
Input: {{printf "%q" .}}
Output 0: {{title .}}
Output 1: {{title . | printf "%q"}}
Output 2: {{printf "%q" . | title}}
`// Create a template, add the function map, and parse the text.
	tmpl, err := template.New("titleTest").Funcs(funcMap).Parse(templateText)if err != nil {
		log.Fatalf("parsing: %s", err)}// Run the template to verify the output.
	err = tmpl.Execute(os.Stdout, "the go programming language")if err != nil {
		log.Fatalf("execution: %s", err)}}

示例(Glob)

这里我们演示从目录加载一组模板。

编码:

// Here we create a temporary directory and populate it with our sample// template definition files; usually the template files would already// exist in some location known to the program.dir := createTestDir([]templateFile{        // T0.tmpl is a plain template file that just invokes T1.        {"T0.tmpl", `T0 invokes T1: ({{template "T1"}})`},        // T1.tmpl defines a template, T1 that invokes T2.        {"T1.tmpl", `{{define "T1"}}T1 invokes T2: ({{template "T2"}}){{end}}`},        // T2.tmpl defines a template T2.        {"T2.tmpl", `{{define "T2"}}This is T2{{end}}`},})// Clean up after the test; another quirk of running as an example.defer os.RemoveAll(dir)// pattern is the glob pattern used to find all the template files.pattern := filepath.Join(dir, "*.tmpl")// Here starts the example proper.// T0.tmpl is the first name matched, so it becomes the starting template,// the value returned by ParseGlob.tmpl := template.Must(template.ParseGlob(pattern))err := tmpl.Execute(os.Stdout, nil)if err != nil {
        log.Fatalf("template execution: %s", err)}

输出:

T0 invokes T1: (T1 invokes T2: (This is T2))

示例(Helpers)

此示例演示了共享某些模板并在不同环境中使用它们的一种方法。在这个变体中,我们手动添加多个驱动模板到现有的一组模板。

编码:

// Here we create a temporary directory and populate it with our sample// template definition files; usually the template files would already// exist in some location known to the program.dir := createTestDir([]templateFile{        // T1.tmpl defines a template, T1 that invokes T2.        {"T1.tmpl", `{{define "T1"}}T1 invokes T2: ({{template "T2"}}){{end}}`},        // T2.tmpl defines a template T2.        {"T2.tmpl", `{{define "T2"}}This is T2{{end}}`},})// Clean up after the test; another quirk of running as an example.defer os.RemoveAll(dir)// pattern is the glob pattern used to find all the template files.pattern := filepath.Join(dir, "*.tmpl")// Here starts the example proper.// Load the helpers.templates := template.Must(template.ParseGlob(pattern))// Add one driver template to the bunch; we do this with an explicit template definition._, err := templates.Parse("{{define `driver1`}}Driver 1 calls T1: ({{template `T1`}})\n{{end}}")if err != nil {
        log.Fatal("parsing driver1: ", err)}// Add another driver template._, err = templates.Parse("{{define `driver2`}}Driver 2 calls T2: ({{template `T2`}})\n{{end}}")if err != nil {
        log.Fatal("parsing driver2: ", err)}// We load all the templates before execution. This package does not require// that behavior but html/template's escaping does, so it's a good habit.err = templates.ExecuteTemplate(os.Stdout, "driver1", nil)if err != nil {
        log.Fatalf("driver1 execution: %s", err)}err = templates.ExecuteTemplate(os.Stdout, "driver2", nil)if err != nil {
        log.Fatalf("driver2 execution: %s", err)}

输出:

Driver 1 calls T1: (T1 invokes T2: (This is T2))Driver 2 calls T2: (This is T2)

示例(Share)

本示例演示如何将一组驱动程序模板与不同的辅助程序模板集一起使用。

编码:

// Here we create a temporary directory and populate it with our sample// template definition files; usually the template files would already// exist in some location known to the program.dir := createTestDir([]templateFile{        // T0.tmpl is a plain template file that just invokes T1.        {"T0.tmpl", "T0 ({{.}} version) invokes T1: ({{template `T1`}})\n"},        // T1.tmpl defines a template, T1 that invokes T2. Note T2 is not defined        {"T1.tmpl", `{{define "T1"}}T1 invokes T2: ({{template "T2"}}){{end}}`},})// Clean up after the test; another quirk of running as an example.defer os.RemoveAll(dir)// pattern is the glob pattern used to find all the template files.pattern := filepath.Join(dir, "*.tmpl")// Here starts the example proper.// Load the drivers.drivers := template.Must(template.ParseGlob(pattern))// We must define an implementation of the T2 template. First we clone// the drivers, then add a definition of T2 to the template name space.// 1. Clone the helper set to create a new name space from which to run them.first, err := drivers.Clone()if err != nil {
        log.Fatal("cloning helpers: ", err)}// 2. Define T2, version A, and parse it._, err = first.Parse("{{define `T2`}}T2, version A{{end}}")if err != nil {
        log.Fatal("parsing T2: ", err)}// Now repeat the whole thing, using a different version of T2.// 1. Clone the drivers.second, err := drivers.Clone()if err != nil {
        log.Fatal("cloning drivers: ", err)}// 2. Define T2, version B, and parse it._, err = second.Parse("{{define `T2`}}T2, version B{{end}}")if err != nil {
        log.Fatal("parsing T2: ", err)}// Execute the templates in the reverse order to verify the// first is unaffected by the second.err = second.ExecuteTemplate(os.Stdout, "T0.tmpl", "second")if err != nil {
        log.Fatalf("second execution: %s", err)}err = first.ExecuteTemplate(os.Stdout, "T0.tmpl", "first")if err != nil {
        log.Fatalf("first: execution: %s", err)}

输出:

T0 (second version) invokes T1: (T1 invokes T2: (T2, version B))T0 (first version) invokes T1: (T1 invokes T2: (T2, version A))

func Must

func Must(t *Template, err error) *Template

Must 必须是一个帮助函数,如果错误不为零,它会将函数调用返回(* Template,error)并发生混乱。它旨在用于变量初始化,如

var t = template.Must(template.New("name").Parse("text"))

func New

func New(name string) *Template

New 使用给定的名称分配一个新的,未定义的模板。

func ParseFiles

func ParseFiles(filenames ...string) (*Template, error)

ParseFiles 创建一个新的模板并从命名文件中解析模板定义。返回的模板名称将具有第一个文件的基本名称和解析内容。必须至少有一个文件。如果发生错误,解析将停止,返回的 * Template 将为零。

当在不同的目录中分析具有相同名称的多个文件时,提到的最后一个将是结果。例如, ParseFiles(“a / foo”,“b / foo”)存储“b / foo”作为名为“foo”的模板,而“a / foo”不可用。

func ParseGlob

func ParseGlob(pattern string) (*Template, error)

ParseGlob 创建一个新的模板并分析模式标识的文件中的模板定义,该文件必须至少匹配一个文件。返回的模板将具有与该模式匹配的第一个文件的(基本)名称和(已分析)内容。 ParseGlob 相当于使用模式匹配的文件列表调用 ParseFiles 。

当在不同的目录中分析具有相同名称的多个文件时,提到的最后一个将是结果。

func (*Template) AddParseTree

func (t *Template) AddParseTree(name string, tree *parse.Tree) (*Template, error)

AddParseTree 为具有给定名称的模板添加解析树并将其与t关联。如果该模板不存在,它将创建一个新模板。如果模板确实存在,它将被替换。

func (*Template) Clone

func (t *Template) Clone() (*Template, error)

Clone 返回模板的副本,包括所有关联的模板。实际表示不会被复制,但是相关模板的名称空间是,因此对副本中的分析进一步调用会将模板添加到副本,但不会添加到原始模板。可以使用克隆来制备通用模板,并在制作克隆后通过添加变体来将它们与其他模板的变体定义一起使用。

func (*Template) DefinedTemplates

func (t *Template) DefinedTemplates() string

DefinedTemplates 返回一个字符串,列出定义的模板,以字符串“;定义的模板为:”为前缀。如果没有,则返回空字符串。用于在这里和 HTML /模板中生成错误消息。

func (*Template) Delims

func (t *Template) Delims(left, right string) *Template

Delims 将动作分隔符设置为指定的字符串,以便在随后调用 Parse,ParseFiles或ParseGlob 时使用。嵌套模板定义将继承这些设置。一个空的分隔符表示相应的默认值:{{或}}。返回值是模板,因此可以将调用链接起来。

func (*Template) Execute

func (t *Template) Execute(wr io.Writer, data interface{}) error

执行将解析的模板应用于指定的数据对象,并将输出写入到 wr。如果执行模板或写入其输出时发生错误,则执行停止,但部分结果可能已写入输出写入器。模板可以并行安全地执行,但如果并行执行共享 Writer ,输出可能会交错。

如果数据是 reflect.Value,则该模板将应用于 reflect.Value 保留的具体值,如 fmt.Print 中所示。

func (*Template) ExecuteTemplate

func (t *Template) ExecuteTemplate(wr io.Writer, name string, data interface{}) error

ExecuteTemplate 将与具有给定名称的t关联的模板应用于指定的数据对象,并将输出写入 wr。如果执行模板或写入其输出时发生错误,则执行停止,但部分结果可能已写入输出写入器。模板可以并行安全地执行,但如果并行执行共享 Writer,输出可能会交错。

func (*Template) Funcs

func (t *Template) Funcs(funcMap FuncMap) *Template

Funcs 将参数图的元素添加到模板的功能图中。它必须在解析模板之前调用。如果映射中的值不是具有适当返回类型的函数,或者该名称不能在语法上作为模板中的函数使用,那么它会发生混乱。覆盖地图的元素是合法的。返回值是模板,因此可以将调用链接起来。

func (*Template) Lookup

func (t *Template) Lookup(name string) *Template

查找将返回与 t 关联的给定名称的模板。如果没有这样的模板或模板没有定义,它将返回 nil。

func (*Template) Name

func (t *Template) Name() string

Name 返回模板的名称。

func (*Template) New

func (t *Template) New(name string) *Template

New 分配一个新的,未定义的模板与给定的和相同的分隔符相关联。该关联是可传递的,它允许一个模板通过 {{template}} 动作调用另一个模板。

func (*Template) Option

func (t *Template) Option(opt ...string) *Template

选项为模板设置选项。选项由字符串描述,可以是简单字符串或“key = value”。选项字符串中最多只能有一个等号。如果选项字符串无法识别或无效,则选项会出现混乱。

已知选项:

missingkey:如果地图使用地图中不存在的键索引,则控制执行过程中的行为。

"missingkey=default" or "missingkey=invalid"
	The default behavior: Do nothing and continue execution.
	If printed, the result of the index operation is the string"<no value>"."missingkey=zero"
	The operation returns the zero value for the map type's element."missingkey=error"
	Execution stops immediately with an error.

func (*Template) Parse

func (t *Template) Parse(text string) (*Template, error)

解析将文本解析为t的模板体。文本中的命名模板定义({{define ...}}或{{block ...}}语句)定义了与 t 关联的附加模板,并从 t 本身的定义中删除。

连续调用 Parse 可以重新定义模板。包含仅包含空格和注释的主体的模板定义被视为空白,并且不会替换现有模板的主体。这允许使用Parse添加新的命名模板定义而不覆盖主模板主体。

func (*Template) ParseFiles

func (t *Template) ParseFiles(filenames ...string) (*Template, error)

ParseFiles 解析命名文件并将生成的模板与 t 关联。如果发生错误,解析将停止并且返回的模板为零;否则是 t。必须至少有一个文件。由于由 ParseFiles 创建的模板是由参数文件的基名命名的,所以 t 通常应该具有文件的(基)名之一的名称。如果没有,则根据调用  ParseFiles 之前的内容,t.Execute 可能会失败。在这种情况下,使用 t.ExecuteTemplate 执行一个有效的模板。

当在不同的目录中分析具有相同名称的多个文件时,提到的最后一个将是结果。

func (*Template) ParseGlob

func (t *Template) ParseGlob(pattern string) (*Template, error)

ParseGlob 解析由模式标识的文件中的模板定义,并将生成的模板与 t 关联。该模式由 filepath.Glob 处理,并且必须至少匹配一个文件。ParseGlob 相当于使用模式匹配的文件列表调用 t.ParseFiles。

当在不同的目录中分析具有相同名称的多个文件时,提到的最后一个将是结果。

func (*Template) Templates

func (t *Template) Templates() []*Template

模板返回一个与 t 关联的定义模板。

子目录

名称

概要

| .. |

|  parse | 打包 parse 构建为 text / template 和 html / template 定义的模板分析树。|

上一篇: 下一篇: