模板

Many languages have mechanisms to convert strings from one form to another. Go has a template mechanism to convert strings based on the content of an object supplied as an argument. While this is often used in rewriting HTML to insert object values, it can be used in other situations. Note that this material doesn't have anything explicitly to do with networking, but may be useful to network programs.

很多编程语言都有字符串之间转换的机制,而GO语言则是通过模板来将一个对象的内容来作为参数传递从而字符串的转换。此方式不仅可以在重写HTML时插入对象值,也适用于其他方面。注意,本章内容并没有明确给出网络的工作方式,但对于网络编程方式很有用处。

Introduction

介绍

Most server-side languages have a mechanism for taking predominantly static pages and inserting a dynamically generated component, such as a list of items. Typical examples are scripts in Java Server Pages, PHP scripting and many others. Go has adopted a relatively simple scripting language in the template package.

大多数服务器端语言的机制主要是在静态页面插入一个动态生成的组件,如清单列表项目。典型的例子是在JSP、PHP和许多其他语言的脚本中。GO的template包中采取了相对简单的脚本化语言。

At the time of writing a new template package has been adopted. There is very little documentation on the template packages. There is a small amount on the old package, which is currently still available in the old/template. There is no documentation on the new package as yet apart from the reference page. The template package changed with r60 (released 2011/09/07).

因为新的template包是刚刚被采用的,所有现在的template包中的文档少的可怜,旧的old/template包中也还存有少量的旧模板。新发布的帮助页面还没有关于新包的文档。关于template包的更改请参阅r60 (released 2011/09/07).

We describe the new package here. The package is designed to take text as input and output different text, based on transforming the original text using the values of an object. Unlike JSP or similar, it is not restricted to HTML files but it is likely to find greatest use there.

在这里,我们描述了这个新包。该包是描述了通过使用对象值改变了原来文本的方式从而在输入和输出时获取不同的文本。与JSP或类似的不同,它的作用不仅限于HTML文件,但在那可能会有更大的作用。

The original source is called a template and will consist of text that is transmitted unchanged, and embedded commands which can act on and change text. The commands are delimited by {{ … }} , similar to the JSP commands <%= … =%> and PHPs .

源文件被称作 template ,包括文本传输方式不变,以嵌入命令可以作用于和更改文本。命令规定如 {{ … }} ,类似于JSP命令 <%= … =%> 和PHP命令

Inserting object values

插入对象值

A template is applied to a Go object. Fields from that Go object can be inserted into the template, and you can 'dig" into the object to find subfields, etc. The current object is represented as '.', so that to insert the value of the current object as a string, you use {{.}}. The package uses the fmt package by default to work out the string used as inserted values.

模板应用于GO对象中.GO对象的字段被插入到模板后,你就能从域中“挖”到他的子域,等等。当前对象以'.'代替, 所以把当前对象当做字符串插入时,你可以采用{{.}}的方式。这个包默认采用 fmt 包来作为插入值的字符串输出。

To insert the value of a field of the current object, you use the field name prefixed by '.'. For example, if the object is of type

要插入当前对象的一个字段的值,你使用的字段名前加前缀 '.'。 例如, 如果要插入的对象的类型为

  1. type Person struct {
  2. Name string
  3. Age int
  4. Emails []string
  5. Jobs []*Jobs
  6. }
  7.  

then you insert the values of Name and Age by

那么你要插入的字段 NameAge 如下

  1. The name is {{.Name}}.
  2. The age is {{.Age}}.
  3.  

We can loop over the elements of an array or other list using the range command. So to access the contents of the Emails array we do

我们可以使用range命令来循环一个数组或者链表中的元素。所以要获取 Emails 数组的信息,我们可以这么干

  1. {{range .Emails}}
  2. ...
  3. {{end}}
  4.  

if Job is defined by

如果Job定义为

  1. type Job struct {
  2. Employer string
  3. Role string
  4. }
  5.  

and we want to access the fields of a Person's Jobs, we can do it as above with a {{range .Jobs}}. An alternative is to switch the current object to the Jobs field. This is done using the {{with …}} … {{end}} construction, where now {{.}} is the Jobs field, which is an array:

如果我们想访问 Person字段中的 Jobs, 我们可以这么干 {{range .Jobs}}。这是一种可以将当前对象转化为Jobs 字段的方式. 通过 {{with …}} … {{end}} 这种方式, 那么{{.}} 就可以拿到Jobs 字段了,如下:

  1. {{with .Jobs}}
  2. {{range .}}
  3. An employer is {{.Employer}}
  4. and the role is {{.Role}}
  5. {{end}}
  6. {{end}}
  7.  

You can use this with any field, not just an array. Using templates

你可以用这种方法操作任何类型的字段,而不仅限于数组。亲,用模板吧!

Once we have a template, we can apply it to an object to generate a new string, using the object to fill in the template values. This is a two-step process which involves parsing the template and then applying it to an object. The result is output to a Writer, as in

当我们拥有了模板,我们将它应用在对象中生成一个字符串,用这个对象来填充这个模板的值。分两步来实现模块的转化和应用,并且输出一个Writer, 如下

  1. t := template.New("Person template")
  2. t, err := t.Parse(templ)
  3. if err == nil {
  4. buff := bytes.NewBufferString("")
  5. t.Execute(buff, person)
  6. }
  7.  

An example program to apply a template to an object and print to standard output is

下面是一个例子来演示模块应用在对象上并且标准输入:

  1. /**
  2. * PrintPerson
  3. */
  4. package main
  5. import (
  6. "fmt"
  7. "html/template"
  8. "os"
  9. )
  10. type Person struct {
  11. Name string
  12. Age int
  13. Emails []string
  14. Jobs []*Job
  15. }
  16. type Job struct {
  17. Employer string
  18. Role string
  19. }
  20. const templ = `The name is {{.Name}}.
  21. The age is {{.Age}}.
  22. {{range .Emails}}
  23. An email is {{.}}
  24. {{end}}
  25. {{with .Jobs}}
  26. {{range .}}
  27. An employer is {{.Employer}}
  28. and the role is {{.Role}}
  29. {{end}}
  30. {{end}}
  31. `
  32. func main() {
  33. job1 := Job{Employer: "Monash", Role: "Honorary"}
  34. job2 := Job{Employer: "Box Hill", Role: "Head of HE"}
  35. person := Person{
  36. Name: "jan",
  37. Age: 50,
  38. Emails: []string{"jan@newmarch.name", "jan.newmarch@gmail.com"},
  39. Jobs: []*Job{&job1, &job2},
  40. }
  41. t := template.New("Person template")
  42. t, err := t.Parse(templ)
  43. checkError(err)
  44. err = t.Execute(os.Stdout, person)
  45. checkError(err)
  46. }
  47. func checkError(err error) {
  48. if err != nil {
  49. fmt.Println("Fatal error ", err.Error())
  50. os.Exit(1)
  51. }
  52. }

The output from this is

输出如下:

  1. The name is jan.
  2. The age is 50.
  3. An email is jan@newmarch.name
  4. An email is jan.newmarch@gmail.com
  5. An employer is Monash
  6. and the role is Honorary
  7. An employer is Box Hill
  8. and the role is Head of HE
  9.  

Note that there is plenty of whitespace as newlines in this printout. This is due to the whitespace we have in our template. If we wish to reduce this, eliminate newlines in the template as in

注意,上面有很多空白的输出,这是因为我们的模板中有很多空白。如果想消除它, 模板设置如下:

  1. {{range .Emails}} An email is {{.}} {{end}}
  2.  

In the example, we used a string in the program as the template. You can also load templates from a file using the function template.ParseFiles(). For some reason that I don't understand (and which wasn't required in earlier versions), the name assigned to the template must be the same as the basename of the first file in the list of files. Is this a bug?

在这个示例例中,我们用字符串应用于模板。你同样也可以用方法template.ParseFiles()来从文件中下载模板。因为某些原因,我还不没搞清楚(在早期版本没有强制要求),关联模板的名字必须要与文件列表的第一个文件的基名相同。话说,这个是BUG吗?

Pipelines

管道

The above transformations insert pieces of text into a template. Those pieces of text are essentially arbitrary, whatever the string values of the fields are. If we want them to appear as part of an HTML document (or other specialised form) then we will have to escape particular sequences of characters. For example, to display arbitrary text in an HTML document we have to change "<" to "&lt;". The Go templates have a number of builtin functions, and one of these is the function html. These functions act in a similar manner to Unix pipelines, reading from standard input and writing to standard output.

上述转换到模板中插入的文本块。这些字符基本上是任意的,是任何字符串的字段值。如果我们希望它们出现的是HTML文档(或其他的特殊形式)的一部分,那么我们将不得不脱离特定的字符序列。例如,要显示任意文本在HTML文档中,我们要将“<”改成“&lt”。GO模板有一些内建函数,其中之一是html。这些函数的作用与Unix的管道类似,从标准输入读取和写入到标准输出。

To take the value of the current object '.' and apply HTML escapes to it, you write a "pipeline" in the template

如果想用“.”来获取当前对象值并且应用于HTML转义,你可以在模板里写个“管道”:

  1. {{. | html}}
  2.  

and similarly for other functions.

其他方法类似。

Mike Samuel has pointed out a convenience function currently in the exp/template/html package. If all of the entries in a template need to be passed through the html template function, then the Go function Escape(t *template.Template) can take a template and add the html function to each node in the template that doesn't already have one. This will be useful for templates used for HTML documents and can form a pattern for similar function uses elsewhere.

Mike Samuel指出,目前在exp/template/html 包里有一个方便的方法。如果所有的模板中的条目需要通过html 模板函数,那么Go语言方法 Escape(t *template.Template)就能获取模板而后将html 函数添加到模板中不存在该函数的每个节点中。用于HTML文档的模板是非常有用的,并能在其他使用场合生成相似的方法模式。

Defining functions

定义方法

The templates use the string representation of an object to insert values, using the fmt package to convert the object to a string. Sometimes this isn't what is needed. For example, to avoid spammers getting hold of email addresses it is quite common to see the symbol '@' replaced by the word " at ", as in "jan at newmarch.name". If we want to use a template to display email addresses in that form, then we have to build a custom function to do this transformation.

模板使用对象化的字符串表示形式插入值,使用fmt包将对象转换为字符串。有时候,这并不是必需。例如,为了避免被垃圾邮件发送者掌握电子邮件地址,常见的方式是把字符号“@”替换为“at”,如“jan at newmarch.name”。如果我们要使用一个模板,显示在该表单中的电子邮件地址,那么我们就必须建立一个自定义的功能做这种转变。

Each template function has a name that is used in the templates themselves, and an associated Go function. These are linked by the type

每个模板函数中使用的模板本身有的一个名称,以及相关联的函数。他们用下面方式进行关联如下

  1. type FuncMap map[string]interface{}
  2.  

For example, if we want our template function to be "emailExpand" which is linked to the Go function EmailExpander then we add this to the functions in a template by

例如,如果我们希望我们的模板函数是“emailExpand”,用来关联到Go函数EmailExpander,然后,我们像这样添加函数到到模板中

  1. t = t.Funcs(template.FuncMap{"emailExpand": EmailExpander})
  2.  

The signature for EmailExpander is typically

EmailExpander通常像这样标记:

  1. func EmailExpander(args ...interface{}) string
  2.  

In the use we are interested in, there should only be one argument to the function which will be a string. Existing functions in the Go template library have some initial code to handle non-conforming cases, so we just copy that. Then it is just simple string manipulation to change the format of the email address. A program is

我们感兴趣的是在使用过程中,那是一个只有一个参数的函数,并且是个字符串。在Go模板库的现有功能有初步的代码来处理不符合要求的情况,所以我们只需要复制。然后,它就能通过简单的字符串操作来改变格式的电子邮件地址。程序如

  1. /**
  2. * PrintEmails
  3. */
  4. package main
  5. import (
  6. "fmt"
  7. "os"
  8. "strings"
  9. "text/template"
  10. )
  11. type Person struct {
  12. Name string
  13. Emails []string
  14. }
  15. const templ = `The name is {{.Name}}.
  16. {{range .Emails}}
  17. An email is "{{. | emailExpand}}"
  18. {{end}}
  19. `
  20. func EmailExpander(args ...interface{}) string {
  21. ok := false
  22. var s string
  23. if len(args) == 1 {
  24. s, ok = args[0].(string)
  25. }
  26. if !ok {
  27. s = fmt.Sprint(args...)
  28. }
  29. // find the @ symbol
  30. substrs := strings.Split(s, "@")
  31. if len(substrs) != 2 {
  32. return s
  33. }
  34. // replace the @ by " at "
  35. return (substrs[0] + " at " + substrs[1])
  36. }
  37. func main() {
  38. person := Person{
  39. Name: "jan",
  40. Emails: []string{"jan@newmarch.name", "jan.newmarch@gmail.com"},
  41. }
  42. t := template.New("Person template")
  43. // add our function
  44. t = t.Funcs(template.FuncMap{"emailExpand": EmailExpander})
  45. t, err := t.Parse(templ)
  46. checkError(err)
  47. err = t.Execute(os.Stdout, person)
  48. checkError(err)
  49. }
  50. func checkError(err error) {
  51. if err != nil {
  52. fmt.Println("Fatal error ", err.Error())
  53. os.Exit(1)
  54. }
  55. }

The output is

输出为:

  1. The name is jan.
  2. An email is "jan at newmarch.name"
  3. An email is "jan.newmarch at gmail.com"
  4.  

Variables

变量

The template package allows you to define and use variables. As motivation for this, consider how we might print each person's email address prefixed by their name. The type we use is again

template包,允许您定义和使用变量。这样做的动机,可能我们会考虑通过把他们的名字当做电子邮件地址前缀打印出来。我们又使用这个类型

  1. type Person struct {
  2. Name string
  3. Emails []string
  4. }
  5.  

To access the email strings, we use a range statement such as

为了访问email的所有字符串, 可以用 range,如下

  1. {{range .Emails}}
  2. {{.}}
  3. {{end}}
  4.  

But at that point we cannot access the Name field as '.' is now traversing the array elements and the Name is outside of this scope. The solution is to save the value of the Name field in a variable that can be accessed anywhere in its scope. Variables in templates are prefixed by '$'. So we write

但是需要指出的是,我们无法用'.' 的形式来访问字段 Name,因为当他被转化成数组元素时,字段Name并不包括其中。解决方法是,将字段Name 存储为一个变量,那么它就能在任意范围内被访问。变量在模板中用法是加前缀'$'。所以可以这样

  1. {{$name := .Name}}
  2. {{range .Emails}}
  3. Name is {{$name}}, email is {{.}}
  4. {{end}}
  5.  

The program is

程序如下:

  1. /**
  2. * PrintNameEmails
  3. */
  4. package main
  5. import (
  6. "html/template"
  7. "os"
  8. "fmt"
  9. )
  10. type Person struct {
  11. Name string
  12. Emails []string
  13. }
  14. const templ = `{{$name := .Name}}
  15. {{range .Emails}}
  16. Name is {{$name}}, email is {{.}}
  17. {{end}}
  18. `
  19. func main() {
  20. person := Person{
  21. Name: "jan",
  22. Emails: []string{"jan@newmarch.name", "jan.newmarch@gmail.com"},
  23. }
  24. t := template.New("Person template")
  25. t, err := t.Parse(templ)
  26. checkError(err)
  27. err = t.Execute(os.Stdout, person)
  28. checkError(err)
  29. }
  30. func checkError(err error) {
  31. if err != nil {
  32. fmt.Println("Fatal error ", err.Error())
  33. os.Exit(1)
  34. }
  35. }

with output

输出为

  1. Name is jan, email is jan@newmarch.name
  2. Name is jan, email is jan.newmarch@gmail.com
  3.  

Conditional statements

条件语句

Continuing with our Person example, supposing we just want to print out the list of emails, without digging into it. We can do that with a template

继续我们那个Person的例子,假设我们只是想打印出来的邮件列表,而不关心其中的字段。我们可以用模板这么干

  1. Name is {{.Name}}
  2. Emails are {{.Emails}}

This will print

  1. Name is jan
  2. Emails are [jan@newmarch.name jan.newmarch@gmail.com]
  3.  

because that is how the fmt package will display a list.

因为这个fmt包会显示一个列表。

In many circumstances that may be fine, if that is what you want. Let's consider a case where it is almost right but not quite. There is a JSON package to serialise objects, which we looked at in Chapter 4. This would produce

在许多情况下,这样做也没有问题,如果那是你想要的。让我们考虑下一种情况,它 几乎是对的但不是必须的。有一个JSON序列化对象的包,让我们看看第4章。它是这样的

  1. {"Name": "jan",
  2. "Emails": ["jan@newmarch.name", "jan.newmarch@gmail.com"]
  3. }
  4.  

The JSON package is the one you would use in practice, but let's see if we can produce JSON output using templates. We can do something similar just by the templates we have. This is almost right as a JSON serialiser:

JSON包是一个你会在实践中使用,但是让我们看看我们是否能够使用JSON输出模板。我们可以做一些我们有的类似的模板。这几乎就是一个JSON串行器:

  1. {"Name": "{{.Name}}",
  2. "Emails": {{.Emails}}
  3. }
  4.  

It will produce

像这样组装

  1. {"Name": "jan",
  2. "Emails": [jan@newmarch.name jan.newmarch@gmail.com]
  3. }
  4.  

which has two problems: the addresses aren't in quotes, and the list elements should be ',' separated.

其中有两个问题:地址没有在引号中,列表中的元素应该是','分隔。

How about this: looking at the array elements, putting them in quotes and adding commas?

这样如何:在数组中的元素,把它们放在引号中并用逗号分隔?

  1. {"Name": {{.Name}},
  2. "Emails": [
  3. {{range .Emails}}
  4. "{{.}}",
  5. {{end}}
  6. ]
  7. }
  8.  

It will produce

像这样组装

  1. {"Name": "jan",
  2. "Emails": ["jan@newmarch.name", "jan.newmarch@gmail.com",]
  3. }
  4.  

(plus some white space.).

(再加上一些空白)。

Again, almost correct, but if you look carefully, you will see a trailing ',' after the last list element. According to the JSON syntax (see http://www.json.org/, this trailing ',' is not allowed. Implementations may vary in how they deal with this.

同样,这样貌似几乎是正确的,但如果你仔细看,你会看到尾有“,”在最后的列表元素。根据JSON的语法(请参阅 http://www.json.org/,这个结尾的','是不允许的。这样实现结果可能会有所不同。

What we want is "print every element followed by a ',' except for the last one." This is actually a bit hard to do, so a better way is "print every element preceded by a ',' except for the first one." (I got this tip from "brianb" at Stack Overflow.). This is easier, because the first element has index zero and many programming languages, including the Go template language, treat zero as Boolean false.

我们想要打印所有在后面带','的元素除了最后一个。"这个确实有点难搞, 一个好方法"在',' 之前打印所有元素除了第一个。" (我在 "brianb"的 Stack Overflow上提了建议)。这样更易于实现,因为第一个元素索引为0,很多编程语言包括GO模板都将0当做布尔型的false

One form of the conditional statement is {{if pipeline}} T1 {{else}} T0 {{end}}. We need the pipeline to be the index into the array of emails. Fortunately, a variation on the range statement gives us this. There are two forms which introduce variables

条件语句的一种形式是{{if pipeline}} T1 {{else}} T0 {{end}}。我们需要通过pipeline来获取电子邮件到数组的索引。幸运的是, range的变化语句为我们提供了这一点。有两种形式,引进变量

  1. {{range $elmt := array}}
  2. {{range $index, $elmt := array}}
  3.  

So we set up a loop through the array, and if the index is false (0) we just print the element, otherwise print it preceded by a ','. The template is

所以我们遍历数组,如果该索引是false(0),我们只是打印的这个索引的元素,否则打印它前面是','的元素。模板是这样的

  1. {"Name": "{{.Name}}",
  2. "Emails": [
  3. {{range $index, $elmt := .Emails}}
  4. {{if $index}}
  5. , "{{$elmt}}"
  6. {{else}}
  7. "{{$elmt}}"
  8. {{end}}
  9. {{end}}
  10. ]
  11. }
  12.  

and the full program is

完整的程序如下

  1. /**
  2. * PrintJSONEmails
  3. */
  4. package main
  5. import (
  6. "html/template"
  7. "os"
  8. "fmt"
  9. )
  10. type Person struct {
  11. Name string
  12. Emails []string
  13. }
  14. const templ = `{"Name": "{{.Name}}",
  15. "Emails": [
  16. {{range $index, $elmt := .Emails}}
  17. {{if $index}}
  18. , "{{$elmt}}"
  19. {{else}}
  20. "{{$elmt}}"
  21. {{end}}
  22. {{end}}
  23. ]
  24. }
  25. `
  26. func main() {
  27. person := Person{
  28. Name: "jan",
  29. Emails: []string{"jan@newmarch.name", "jan.newmarch@gmail.com"},
  30. }
  31. t := template.New("Person template")
  32. t, err := t.Parse(templ)
  33. checkError(err)
  34. err = t.Execute(os.Stdout, person)
  35. checkError(err)
  36. }
  37. func checkError(err error) {
  38. if err != nil {
  39. fmt.Println("Fatal error ", err.Error())
  40. os.Exit(1)
  41. }
  42. }

This gives the correct JSON output.

上面给出的是正确的JSON输出

Before leaving this section, we note that the problem of formatting a list with comma separators can be approached by defining suitable functions in Go that are made available as template functions. To re-use a well known saying, "There's more than one way to do it!". The following program was sent to me by Roger Peppe:

在结束本节之前,我们强调了用逗号分隔的列表格式的问题,解决方式是可以在模板函数中定义适当的函数。正如俗话说的,“道路不止一条!”下面的程序是Roger Peppe给我的:

  1. /**
  2. * Sequence.go
  3. * Copyright Roger Peppe
  4. */
  5. package main
  6. import (
  7. "errors"
  8. "fmt"
  9. "os"
  10. "text/template"
  11. )
  12. var tmpl = `{{$comma := sequence "" ", "}}
  13. {{range $}}{{$comma.Next}}{{.}}{{end}}
  14. {{$comma := sequence "" ", "}}
  15. {{$colour := cycle "black" "white" "red"}}
  16. {{range $}}{{$comma.Next}}{{.}} in {{$colour.Next}}{{end}}
  17. `
  18. var fmap = template.FuncMap{
  19. "sequence": sequenceFunc,
  20. "cycle": cycleFunc,
  21. }
  22. func main() {
  23. t, err := template.New("").Funcs(fmap).Parse(tmpl)
  24. if err != nil {
  25. fmt.Printf("parse error: %v\n", err)
  26. return
  27. }
  28. err = t.Execute(os.Stdout, []string{"a", "b", "c", "d", "e", "f"})
  29. if err != nil {
  30. fmt.Printf("exec error: %v\n", err)
  31. }
  32. }
  33. type generator struct {
  34. ss []string
  35. i int
  36. f func(s []string, i int) string
  37. }
  38. func (seq *generator) Next() string {
  39. s := seq.f(seq.ss, seq.i)
  40. seq.i++
  41. return s
  42. }
  43. func sequenceGen(ss []string, i int) string {
  44. if i >= len(ss) {
  45. return ss[len(ss)-1]
  46. }
  47. return ss[i]
  48. }
  49. func cycleGen(ss []string, i int) string {
  50. return ss[i%len(ss)]
  51. }
  52. func sequenceFunc(ss ...string) (*generator, error) {
  53. if len(ss) == 0 {
  54. return nil, errors.New("sequence must have at least one element")
  55. }
  56. return &generator{ss, 0, sequenceGen}, nil
  57. }
  58. func cycleFunc(ss ...string) (*generator, error) {
  59. if len(ss) == 0 {
  60. return nil, errors.New("cycle must have at least one element")
  61. }
  62. return &generator{ss, 0, cycleGen}, nil
  63. }

Conclusion

结论

The Go template package is useful for certain kinds of text transformations involving inserting values of objects. It does not have the power of, say, regular expressions, but is faster and in many cases will be easier to use than regular expressions

template包在对于某些类型的文本转换涉及插入对象值的情况是非常有用的。虽然它没有正则表达式功能强大,但它执行比正则表达式速度更快,在许多情况下比正则表达式更容易使用。


Copyright © Jan Newmarch, jan@newmarch.name

If you like this book, please contribute using Flattr

or donate using PayPal

模板 - 图1