Nesting Templates
GoHT composes templates with @render, @children, and @slot.
@rendercalls another template and passes any required parameters.- Nested content under
@renderis passed to the rendered template. @childrenrenders nested content that was passed by the caller.@slotrenders named slot content, optionally with default content.
These directives work in Haml, Slim, and EGO templates. EGO uses command tags such as <%@render ... %> and <%@slot ... %>.
@render
Use @render when one template should call another template.
The same pattern in Slim:
And in EGO:
The parameters passed to @render are the parameters of the generated Go template function. If Page is generated as func Page(title string) goht.Template, then @render Page("Home") calls that generated function and renders the returned template.
Nested render content and @children
Nested content under @render remains in the calling template’s scope. The rendered template chooses where that content appears by using @children.
Rendered output from ArticlePage("Sam") is shaped like:
Slim uses the same = @render and = @children directives:
EGO uses command tags:
Named slots
Named slots are useful for layouts that have several independent regions. A slot renders content that the caller passes with .Slot("name").
The header, main, and footer slots above include default content. GoHT renders default slot content only when the caller does not provide content for that slot. The sidebar and notifications slots render nothing unless the caller provides matching slotted templates.
Slim slot declarations follow the same shape:
EGO slots use <%@slot ... %> or a block form when they have defaults:
Passing slot content
Pass slot content from Go code by calling .Slot("name") on a template and passing it to Render.
Slotted templates can contain their own slots. This lets a page fill a layout slot and still expose nested regions inside that slot.
In this example, ProjectPage(project) fills the layout’s main slot. The ProjectSummary and ProjectActivity templates fill slots that are declared inside ProjectPage.
@attributes is not a composition directive
@attributes expands dynamic attributes inside Haml and Slim attribute lists. It is not used to call templates or pass child content.
See Dynamic Attributes for details.