I had to research a bit to learn how to use the template blocks in Go 1.6 so I wrote up a quick article.
http://www.josephspurrier.com/how-to-use-template-blocks-in-go-1-6/
评论:
togamans:
motojo:Thanks, this is super helpful. What do you use templates for at your company? I built a dashboard with templates as a pre-alpha demo, but never again. I only use them for dynamic content in email notifications I generate now.
togamans:When I build websites, I use templates for each of the pages. You can see how I use templates in my project on Github: https://github.com/josephspurrier/gowebapp/tree/master/template
motojo:yea, when the site got more complex (ie just more things on the page), the templates became unwieldy and the data objects I had to pass in for dynamic content werent great either. I'm in the process of reimplementing the web UI in either angular or react, powered by a json API. Thanks for linking to your project!
saturn_vk:No problem! Using a angular or react with an API is great way to go. That way you have an API that can be used for web or mobile. I think I need to modify my view package to allow templates to be in different folders so it's easier to organize and manage.
motojo:The article is incorrect regarding the limits of the template section. You can omit a define action and still render correctly. In order to do so, you will have to place an empty define action for any template that might be missing, in the base template or any other that will always be included:
https://github.com/urandom/readeef/blob/master/templates/base.tmpl#L20
:Ah, very interesting. I didn't know you could add define actions to the base template when using the template action. So I guess the main difference between template and block is when using template, define actions are required or else it will throw an error. Are there any other differences?
guenther_mit_haar:[deleted]
gohacker:Look on saturn_vk code - you can define the empty sections in the base template to avoid this problem
Yes. And it wasn't clear to me since I read about the new template blocks, what are the benefits apart from introducing a new syntax just for the heck of it?
