HUGO
Menu
GitHub 90640 stars Mastodon

return

Terminates execution of the current template and, when used within a partial template, returns the given value, if any.
Syntax
return [VALUE]
Returns
any

Usage

The return statement terminates execution of the current template and, when used within a partial template, returns the given value, if any. It is a non-standard extension to Go’s text/template package. The statement terminates execution of the current template only. Execution continues in any calling template.

New in v0.166.0

In earlier versions, the return statement was only supported within partial templates, limited to one return statement per template, executed regardless of its position within logical blocks. The return statement now follows normal flow control. You can use it in any template, in any position, any number of times, and Hugo executes it only when reached.

Within a partial template, the return statement may return a value of any data type, such as bool, float, int, map, resource, slice, or string. When returning a value, Hugo discards any output rendered before the return statement. Using the return statement with a value in any other template type fails the build.

Use the return statement without a value to stop execution of the current template in any template type, such as when a page is a draft or when a validation check fails.

The return statement must be the last command in a pipeline. For example, this code fails the build:

{{ return "even" | strings.ToUpper }}

Examples

This example defines a partial template that returns a string value, then calls it from a page template:

layouts/_partials/parity.html
{{ if math.ModBool . 2 }}
  {{ return "even" }}
{{ end }}
{{ return "odd" }}
layouts/single.html
{{ partial "parity.html" 42 }} → even

A partial template that returns the cover image of the first page in a section that has one:

layouts/_partials/section-cover.html
{{ range .Pages }}
  {{ with .Resources.GetMatch "cover.*" }}
    {{ return . }}
  {{ end }}
{{ end }}
layouts/section.html
{{ with partial "section-cover.html" . }}
  <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}

Stopping execution of a page template if the page is a draft:

layouts/single.html
<h2>{{ .Title }}</h2>
{{ if .Draft }}
  <p>This article is a draft.</p>
  {{ return }}
{{ end }}
{{ .Content }}

Stopping execution of a shortcode template after each failed validation check, avoiding deeply nested conditional blocks:

layouts/_shortcodes/img.html
{{ if not (.Get "src") }}
  {{ errorf "The %q shortcode requires a src argument. See %s" .Name .Position }}
  {{ return }}
{{ end }}

{{ if not (.Get "alt") }}
  {{ errorf "The %q shortcode requires an alt argument. See %s" .Name .Position }}
  {{ return }}
{{ end }}

<img src="{{ .Get "src" }}" alt="{{ .Get "alt" }}">