hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

examplefunc_test.go (1601B)

    1 // Copyright 2012 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 //go:build go1.13
    6 // +build go1.13
    7 
    8 package template_test
    9 
   10 import (
   11 	"log"
   12 	"os"
   13 	"strings"
   14 	"text/template"
   15 )
   16 
   17 // This example demonstrates a custom function to process template text.
   18 // It installs the strings.Title function and uses it to
   19 // Make Title Text Look Good In Our Template's Output.
   20 func ExampleTemplate_func() {
   21 	// First we create a FuncMap with which to register the function.
   22 	funcMap := template.FuncMap{
   23 		// The name "title" is what the function will be called in the template text.
   24 		"title": strings.Title,
   25 	}
   26 
   27 	// A simple template definition to test our function.
   28 	// We print the input text several ways:
   29 	// - the original
   30 	// - title-cased
   31 	// - title-cased and then printed with %q
   32 	// - printed with %q and then title-cased.
   33 	const templateText = `
   34 Input: {{printf "%q" .}}
   35 Output 0: {{title .}}
   36 Output 1: {{title . | printf "%q"}}
   37 Output 2: {{printf "%q" . | title}}
   38 `
   39 
   40 	// Create a template, add the function map, and parse the text.
   41 	tmpl, err := template.New("titleTest").Funcs(funcMap).Parse(templateText)
   42 	if err != nil {
   43 		log.Fatalf("parsing: %s", err)
   44 	}
   45 
   46 	// Run the template to verify the output.
   47 	err = tmpl.Execute(os.Stdout, "the go programming language")
   48 	if err != nil {
   49 		log.Fatalf("execution: %s", err)
   50 	}
   51 
   52 	// Output:
   53 	// Input: "the go programming language"
   54 	// Output 0: The Go Programming Language
   55 	// Output 1: "The Go Programming Language"
   56 	// Output 2: "The Go Programming Language"
   57 }