Closed vimdude closed 9 years ago
typewriter’s template.Parse
returns a regular stdlib *template.Template
, on which you can call Funcs. I think that’s equivalent?
I haven’t used FuncMap personally, am unfamiliar with the behavior, feel free to correct me.
Yes you're correct but without running the template through the function first, your template.Parse will fail saying "ToLower" is undefined function. So the following code: // Parse parses (converts) a typewriter.Template to a _template.Template func (tmpl *Template) Parse() (_template.Template, error) { return template.New(tmpl.Name).Parse(tmpl.Text) }
Needs to change to: // Parse parses (converts) a typewriter.Template to a _template.Template func (tmpl *Template) Parse() (_template.Template, error) { return template.New(tmpl.Name).Funcs(tmpl.funcMap).Parse(tmpl.Text) } // tmpl.funcMap will need a set function to assign the function map
Ah, I see. That seems reasonable. I mocked it up locally and the tests pass.
Feel free to send in a pull request (with a test) or I’ll do it when I get a chance.
I created a typewriter for my needs, however I run into issues with import as the typewriter doesn't allow currently to inject a function to a template like the following:
{{ .Type | ToLower }}.{{.Type}}
So that the generated file looks like something like contact.Contact.
However I was able to copy the ByTagValue function and created the local function:
func CustomByTagValue(ts typewriter.TemplateSlice, t typewriter.Type, v typewriter.TagValue) (*template.Template, error) { .... funcMap := template.FuncMap{ "ToLower": func(t typewriter.Type) string { return strings.ToLower(t.Name) }, } // eagerly return on success return template.New(tmpl.Name).Funcs(funcMap).Parse(tmpl.Text) ....
}
If you change your clipperhouse/typewriter/template.go Parse method to take FuncMap as argument, it won't be a problem, at least for me. Thanks for the great work.