text/template printf behaviour

1,879 views
Skip to first unread message

gocss

unread,
Nov 20, 2015, 11:49:55 AM11/20/15
to golang-nuts
having a problem understanding text/template printf behaviour:
the following code originates from
https://golang.org/pkg/text/template/#example_Template_func
minimal changes I made include:
added "tottile" function
alter output2  > Output 2: {{printf "%s%s%s" . . .| title | totitle | title }}
added > output 3: {{title . }}{{totitle . }}{{title . }}
--------
program is below with output results.

why should not output 2 match output 3?
if they should match is this a known bug ?

paul
------------------

package main

import (
    "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,
        "totitle": strings.ToTitle,
    }

    // 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 2: {{printf "1:%q 2:%q 3:%q" . . .| title | totitle | title }}
Output 0: {{title .}}
Output 1: {{title . | printf "%q"}}
Output 2: {{printf "%s%s%s" . . .| title | totitle | title }}
output 3: {{title . }}{{totitle . }}{{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)
    }

}

program results:

Input: "the go programming language"
Output 2: 1:"THE GO PROGRAMMING LANGUAGE" 2:"THE GO PROGRAMMING LANGUAGE" 3:"THE GO PROGRAMMING LANGUAGE"
Output 0: The Go Programming Language
Output 1: "The Go Programming Language"
Output 2: THE GO PROGRAMMING LANGUAGETHE GO PROGRAMMING LANGUAGETHE GO PROGRAMMING LANGUAGE
output 3: The Go Programming LanguageTHE GO PROGRAMMING LANGUAGEThe Go Programming Language

Program exited.
-------------

Andrio Almeida

unread,
Nov 20, 2015, 6:35:11 PM11/20/15
to golang-nuts
Im using a simpler way of templating in GO
like this example:

import (
    "html/template"
)

//init templating data
var tpdata = make(map[string]template.HTML)

//read from disk all templantes html files in index dir, and cache it to the memory
// with all the tags i need,  {{ .TOPMENU }}, {{ .MSG }}, {{ .PAGETITLE.}}, etc...
var tp = map[string]*template.Template{
"index":  template.Must(template.ParseGlob("./templates/index/*")),


// than just put contend in the tags
tpdata["TOPMENU"] = template.HTML("Menu content, links, etc...")
tpdata["MSG"] = template.HTML("Hi, this my mesage to the page")
tpdata["PAGETITLE"] = template.HTML("My webpage")
...
...

//render the page content index, and all my tags on tpdata
tp["index"].ExecuteTemplate(w, "base", tpdata)

gocss

unread,
Nov 23, 2015, 6:42:00 PM11/23/15
to golang-nuts
changed const templateText as follow:

const templateText = `
Input: {{printf "%q" .}}
Output 0: {{title .}}
Output 1: {{title . | printf "%q"}}
Output 2: {{printf "%q %q %q" (title .) (totitle .) (title .)}}
`
which uses parenthesis  in Output 2 ... I was not aware one could invoke functions as such, it behaves now as I would it expect too.
output is:
Input: "the go programming language"
Output 0: The Go Programming Language
Output 1: "The Go Programming Language"
Output 2: "The Go Programming Language" "THE GO PROGRAMMING LANGUAGE" "The Go Programming Language"
Reply all
Reply to author
Forward
0 new messages