5 ms·
Templates are definitely the most lacking aspect of Go in my experience. Templating exists and works but in my usage at least has felt very clunky. It could be
by brightball 11y ago
Templates are definitely the most lacking aspect of Go in my experience. Templating exists and works but in my usage at least has felt very clunky.
It could be that I just haven't come across any resources that do a good job of explaining their strengths. I see so many comments from people who seem genuinely excited about Go's built in templates that my assumption is always that I'm doing something very wrong.
- buro9 11y agoThey are woefully undocumented. Well, that perhaps isn't fair... fairer would be to say that there isn't an authoritative set of examples showing how people can achieve things similar to something like Django templating or Rails templating. It is actually fairly effortless to nest templates, extend them with custom functions, and all of the things one would need to do. Probably one of the best resources is Hugo: https://gohugo.io/templates/go-templates/ https://gohugo.io/templates/go-templates/ and the funcs (template tags) they ship. That isn't too bad at all, but the snippets and examples are so small that it's hard to really see what the value is and how it all glues together to create nice readable templates. Anyhow, the key place to look at template tags would be: 1. Hugo's source code for template funcs: https://github.com/spf13/hugo/blob/master/tpl/template_funcs.go https://github.com/spf13/hugo/blob/master/tpl/template_funcs... 2. Hugo's creation of the func map: https://github.com/spf13/hugo/blob/master/tpl/template_funcs.go#L1644 https://github.com/spf13/hugo/blob/master/tpl/template_funcs... 3. Hugo's documentation on the funcs: https://gohugo.io/templates/functions/ https://gohugo.io/templates/functions/ Those three things, viewed as one, should allow any Go dev to extend templates to do whatever you want them to do. Hugo have been very "functional" in their choice of template tags, but there's huge scope for new packages to deliver more aesthetic tags, or tags for localization, to humanise time and plurals, basically to provide a core resource of tags similar to https://docs.djangoproject.com/en/1.9/ref/templates/builtins/ https://docs.djangoproject.com/en/1.9/ref/templates/builtins... so that each dev doesn't implement their own.
- reitanqild 11y agoOne thing I haven't found how to do is telling a template to insert itself into a parent template, e.g. (pseudotemplate code) <extends main_with_feature_x> <define title {{something}}/> <define body> ... body goes here... </define> etc etc Including everything in every template just reeks of old school php. (Yes, I defend modern php : )
- pram 11y agoThen you didnt look very hard http://play.golang.org/p/DHk9iKnOMO http://play.golang.org/p/DHk9iKnOMO
- reitanqild 11y ago?
- grey-area 11y agoYou can use the template function to include another named template.
- reitanqild 11y agoI don't know if I understand it correctly but I'm specifically looking to avoid every template looking like this: (old school php like) <?php include (header.php)?> ... business logic goes here... <?php include (footer.php)?>
- grey-area 11y agoI agree it is much nicer to have a layout template which includes your view templates (ala rails and many other systems), rather than including header/footer in each template. Yes you can do this, I handle this by having a layout template which includes a key for content, and rendering each view into its own template (without header/footer) which then goes into the content key of the layout. This is pretty straightforward to do though apparently not a usage they anticipated. They've recently introduced the concept of blocks too which is an attempt to solve this though it looked pretty clunky to me last time I looked. See this discussion from author of hugo asking how to use it (always a bad sign!): https://github.com/golang/go/issues/14285 https://github.com/golang/go/issues/14285 I think I prefer just rendering the view separately and then rendering it into the layout, it's easy to do.