Open kevinburke opened 7 years ago
What do you think about this approach?
https://astaxie.gitbooks.io/build-web-application-with-golang/en/07.4.html#nested-templates
Thanks @astaxie for the book.
That looks good! My worries with it are I think having separate templates for the header and the footer is a little overkill - especially if a div starts in the header and is closed in the footer it can be a pain to modify two separate files to update that. It would be better if there was a base template that yielded somehow.
Second is that you have to call ExecuteTemplate three times to get your template to render. It would be nice to just call Execute once and have everything work out okay.
Yes I agree with you. That's in fact the way I do templates in Golang.
Here's an example I just created: https://github.com/brunocassol/golang_nested_template_example
In that example the header and footer are in the same file. It is much easier to follow and eliminates multiple calls to ExecuteTemplate() as you commented.
If you have more than one template, you probably want them to share a header and a footer. Unfortunately that's not trivial to set up.
It would be nice to have an example showing how to do this.