How to Use Template Blocks in Go 1.6

blov · 2016-01-24 02:21:17 · 941 次点击    
这是一个分享于 2016-01-24 02:21:17 的资源,其中的信息可能已经有所发展或是发生改变。

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:

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.

motojo:

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

togamans:

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!

motojo:

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.

saturn_vk:

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

motojo:

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?

:

[deleted]

guenther_mit_haar:

Look on saturn_vk code - you can define the empty sections in the base template to avoid this problem

gohacker:

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?


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

941 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传