Re: [go-nuts] Customized Go template FuncMap example

2017-07-15 Thread Tong Sun
Thanks. I don't know what kind of goals you are trying to achieve, but here are mine: What I'm trying to do is to modulelize customized Go template functions, say one module provide advanced regexp support, while another one specialized in text formatting (vertical aligning, text folding in colum

Re: [go-nuts] Customized Go template FuncMap example

2017-07-15 Thread Matt Ho
Given that text and html templates are separate calls anyways, why not just make separate calls for your custom funcmap? For example: package template import ( html "html/template" text "text/template" ) func Html(name string, fm html.FuncMap) *html.Template { return html.New(name).Funcs

Re: [go-nuts] Customized Go template FuncMap example

2017-07-14 Thread Tong Sun
Gotya. Thanks for all the helps! On Fri, Jul 14, 2017 at 9:27 PM, Matt Harden wrote: > But you also then have no choice but to wrap every single method that > (that you want to also have) returns a *Template, so that it returns your > own template type instead. That's why I don't think it's a go

Re: [go-nuts] Customized Go template FuncMap example

2017-07-14 Thread Matt Harden
If the type of the map is map[string]interface{}, it can be used with either type of template. https://play.golang.org/p/-sFMhKOtNN If you create your own template type, then you can also create your own FuncMap type, and in your own Funcs method, pass it on to the underlying template: type FuncM

Re: [go-nuts] Customized Go template FuncMap example

2017-07-14 Thread Tong Sun
On Thu, Jul 13, 2017 at 10:10 PM, Matt Harden wrote: > Can you do what you want just by building a number of template.FuncMap > values, and then to use them the user just calls > t.Funcs(module1.FuncMap).Funcs(module2.FuncMap), > adding the function maps they want that way? > Yeah, sure, but f

Re: [go-nuts] Customized Go template FuncMap example

2017-07-13 Thread Matt Harden
Can you do what you want just by building a number of template.FuncMap values, and then to use them the user just calls t.Funcs(module1.FuncMap).Funcs(module2.FuncMap), adding the function maps they want that way? I don't think you need your own custom version of the Template type. On Sun, Jul 9,

Re: [go-nuts] Customized Go template FuncMap example

2017-07-09 Thread Tong Sun
Thanks Matt. What I'm trying to do is to modulelize customized Go template functions, say one module provide advanced regexp support, while another one specialized in text formatting (vertical aligning, text folding in column, etc), so on and so forth, *each of the module has a collection of their

Re: [go-nuts] Customized Go template FuncMap example

2017-07-09 Thread Matt Harden
Why are you trying to do that? It feels like you're trying to do object-oriented programming in Go. Don't do that. What are you trying to achieve that the *template.Template type doesn't allow? If you just want a template with a FuncMap already applied, write a function to do that: func MyTemplat

[go-nuts] Customized Go template FuncMap example

2017-07-06 Thread Tong Sun
I'm trying my best to make the following Go template FuncMap example works: https://play.golang.org/p/1-JYseLBUF If I comment out line 55, 56 and un-comment line 54, it works just fine. What I was trying to do is to un-comment line 31, and use DefaultFuncs() or something alike to replace line