github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/tpl/internal/go_templates/texttemplate/example_test.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build go1.13
     6  
     7  package template_test
     8  
     9  import (
    10  	"log"
    11  	"os"
    12  	"strings"
    13  	"text/template"
    14  )
    15  
    16  func ExampleTemplate() {
    17  	// Define a template.
    18  	const letter = `
    19  Dear {{.Name}},
    20  {{if .Attended}}
    21  It was a pleasure to see you at the wedding.
    22  {{- else}}
    23  It is a shame you couldn't make it to the wedding.
    24  {{- end}}
    25  {{with .Gift -}}
    26  Thank you for the lovely {{.}}.
    27  {{end}}
    28  Best wishes,
    29  Josie
    30  `
    31  
    32  	// Prepare some data to insert into the template.
    33  	type Recipient struct {
    34  		Name, Gift string
    35  		Attended   bool
    36  	}
    37  	var recipients = []Recipient{
    38  		{"Aunt Mildred", "bone china tea set", true},
    39  		{"Uncle John", "moleskin pants", false},
    40  		{"Cousin Rodney", "", false},
    41  	}
    42  
    43  	// Create a new template and parse the letter into it.
    44  	t := template.Must(template.New("letter").Parse(letter))
    45  
    46  	// Execute the template for each recipient.
    47  	for _, r := range recipients {
    48  		err := t.Execute(os.Stdout, r)
    49  		if err != nil {
    50  			log.Println("executing template:", err)
    51  		}
    52  	}
    53  
    54  	// Output:
    55  	// Dear Aunt Mildred,
    56  	//
    57  	// It was a pleasure to see you at the wedding.
    58  	// Thank you for the lovely bone china tea set.
    59  	//
    60  	// Best wishes,
    61  	// Josie
    62  	//
    63  	// Dear Uncle John,
    64  	//
    65  	// It is a shame you couldn't make it to the wedding.
    66  	// Thank you for the lovely moleskin pants.
    67  	//
    68  	// Best wishes,
    69  	// Josie
    70  	//
    71  	// Dear Cousin Rodney,
    72  	//
    73  	// It is a shame you couldn't make it to the wedding.
    74  	//
    75  	// Best wishes,
    76  	// Josie
    77  }
    78  
    79  // The following example is duplicated in html/template; keep them in sync.
    80  
    81  func ExampleTemplate_block() {
    82  	const (
    83  		master  = `Names:{{block "list" .}}{{"\n"}}{{range .}}{{println "-" .}}{{end}}{{end}}`
    84  		overlay = `{{define "list"}} {{join . ", "}}{{end}} `
    85  	)
    86  	var (
    87  		funcs     = template.FuncMap{"join": strings.Join}
    88  		guardians = []string{"Gamora", "Groot", "Nebula", "Rocket", "Star-Lord"}
    89  	)
    90  	masterTmpl, err := template.New("master").Funcs(funcs).Parse(master)
    91  	if err != nil {
    92  		log.Fatal(err)
    93  	}
    94  	overlayTmpl, err := template.Must(masterTmpl.Clone()).Parse(overlay)
    95  	if err != nil {
    96  		log.Fatal(err)
    97  	}
    98  	if err := masterTmpl.Execute(os.Stdout, guardians); err != nil {
    99  		log.Fatal(err)
   100  	}
   101  	if err := overlayTmpl.Execute(os.Stdout, guardians); err != nil {
   102  		log.Fatal(err)
   103  	}
   104  	// Output:
   105  	// Names:
   106  	// - Gamora
   107  	// - Groot
   108  	// - Nebula
   109  	// - Rocket
   110  	// - Star-Lord
   111  	// Names: Gamora, Groot, Nebula, Rocket, Star-Lord
   112  }