Nesting Templates

GoHT allows you to nest templates into each other. It provides two directives to do this: @render and @children.

@render

To include another template in the current template, you use the @render directive. The @render directive takes the template name and parameters. In the generated Go code it will be replaced with a call to the other templates generated version.

@goht myTemplate() {
%p
  The following content comes from another template:
  @render otherTemplate()
}

Depending on the otherTemplate it might render as:

<p>
  The following content comes from another template:
  <div class="other">
    <p>Some content</p>
  </div>
</p>

You can also render out content into the other template that has access to the current template scope.

@goht myTemplate() {
- content := "My content"
%p
  The following content comes from another template:
  @render otherTemplate()
    %p= content
}

Depending on the otherTemplate it might render as:

<p>
  The following content comes from another template:
  <div class="other">
    <p>Some content</p>
	<p>My content</p>
  </div>
</p>

The <p>My content</p> is rendered into the otherTemplate and has access to the content variable from the calling template. This is possible because the otherTemplate is using the @children directive.

@children

To include content from the calling template in the current template, you use the @children directive. It will be replaced with the content that has been passed to the template from the calling template.

@goht otherTemplate() {
.other
  %p Some content
  =@children
}

When the otherTemplate is called from the myTemplate it might render as:

<div class="other">
  <p>Some content</p>
  <p>My content</p>
</div>