From 690b0f8ff5795318dfa3834a5a75d6623e7d934a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 1 May 2017 18:40:34 +0200 Subject: tpl: Add docshelper for template funcs And fix some other minor related issues. Updates #3418 --- tpl/fmt/fmt.go | 13 +++++++------ tpl/fmt/init.go | 35 +++++++++++++++++++++++++---------- 2 files changed, 32 insertions(+), 16 deletions(-) (limited to 'tpl/fmt') diff --git a/tpl/fmt/fmt.go b/tpl/fmt/fmt.go index 5e320fede..ca31ec522 100644 --- a/tpl/fmt/fmt.go +++ b/tpl/fmt/fmt.go @@ -26,14 +26,15 @@ func New() *Namespace { type Namespace struct { } -func (ns *Namespace) Print(a ...interface{}) (n int, err error) { - return _fmt.Print(a...) +func (ns *Namespace) Print(a ...interface{}) string { + return _fmt.Sprint(a...) } -func (ns *Namespace) Printf(format string, a ...interface{}) (n int, err error) { - return _fmt.Printf(format, a...) +func (ns *Namespace) Printf(format string, a ...interface{}) string { + return _fmt.Sprintf(format, a...) + } -func (ns *Namespace) Println(a ...interface{}) (n int, err error) { - return _fmt.Println(a...) +func (ns *Namespace) Println(a ...interface{}) string { + return _fmt.Sprintln(a...) } diff --git a/tpl/fmt/init.go b/tpl/fmt/init.go index 0f4296263..98070b777 100644 --- a/tpl/fmt/init.go +++ b/tpl/fmt/init.go @@ -24,18 +24,33 @@ func init() { f := func(d *deps.Deps) *internal.TemplateFuncsNamespace { ctx := New() - examples := [][2]string{ - {`{{ print "works!" }}`, `works!`}, - {`{{ printf "%s!" "works" }}`, `works!`}, - {`{{ println "works!" }}`, "works!\n"}, + ns := &internal.TemplateFuncsNamespace{ + Name: name, + Context: func() interface{} { return ctx }, } - return &internal.TemplateFuncsNamespace{ - Name: name, - Context: func() interface{} { return ctx }, - Aliases: map[string]interface{}{}, - Examples: examples, - } + ns.AddMethodMapping(ctx.Print, + []string{"print"}, + [][2]string{ + {`{{ print "works!" }}`, `works!`}, + }, + ) + + ns.AddMethodMapping(ctx.Println, + []string{"println"}, + [][2]string{ + {`{{ println "works!" }}`, "works!\n"}, + }, + ) + + ns.AddMethodMapping(ctx.Printf, + []string{"printf"}, + [][2]string{ + {`{{ printf "%s!" "works" }}`, `works!`}, + }, + ) + + return ns } -- cgit v1.2.3