Hacker News new | ask | show | jobs
by ckdot2 918 days ago
Both is possible. See here an example how to implement use case 2: https://github.com/ckilb/golang-layout-tpl-example

I don't like Go's template engine too much, I find it a bit cumbersome in many cases. But it's quite capable.

2 comments

Here's a blog post which goes more into detail: https://kilb.tech/golang-templates
That is not quite a true "slot" behavior because the containing pages have to have a reference to the main layout via `{{ define "main" }}`
You're right. But this only means that it won't be possible that you use the same template for different slots. Personally, I don't remember I ever needed that. Actually, most template engines I know work this way (https://twig.symfony.com/doc/2.x/templates.html#template-inh..., "{% block content %}"). But still, it's easy to archieve that by just creating another sub template. So your first template file is the layout, the second one contains the main block definition, and that definition you just include another sub template which can be reused elsewhere.