HUGO

Returning values from partials

The docs are a bit unclear to me.

says:

In order to return a value, a partial must include a lone return statement.

A lone return statement is AFAIK:

{{ return }}

However the examples show

{{ return $value }}

(which makes more sense)

The docs also say (emphasis mine):

Only one return statement is allowed per partial file.

This would imply that –

  • you cannot have {{ if ... }}{{ return $this }}{{ else }}{{ return $that }}{{ end }}
  • you cannot have multiple inline partials that return values (not even a single one if the outer partial also returns a value

Some experimenting. This works (returns the string "foo"):

{{ if 1 }}
{{ return "foo" }}
{{ else }}
{{ return "bar" }}
{{ end }}

But this yields an error:

{{ if 0 }}
{{ return "foo" }}
{{ else }}
{{ return "bar" }}
{{ end }}

==> executing "partials/ret.html" at <return>: wrong number of args for return: want 0 got 1

Much to my astonishment:

{{ if 0 }}
{{ return "foo" }}
{{ end }}

still returns "foo", even though the condition is false.

(This is hugo v0.72.0, haven’t been able to upgrade to latest)

1 Like

English is not my first language, but in this situation lone means “only one”. So you must end your partial with this one return statement.

Fair enough, but still ambiguous. I makes me believe that the return is statically parsed and executed, not dynamically. In other words, it is not a statement like the others.

This really scares the shit out of me:

{{ if 0 }}
{{ $foo := 42 }}
{{ return $foo }}
{{ end }}

The variable assignment is skipped due to the if condition, but the return is executed unconditionally (hence generates an error executing "partials/ret.html" at <$foo>: undefined variable: $foo)

(hugo 0.74.1)

1 Like

Thanks for sharing this. I wasn’t aware of this behavior. Seems like we should either implement return similar to other languages, or throw an error if it is used where it shouldn’t be.

We, or I, implemented return on top of Go templates in the simplest way possible (as in: 2 hours work compared to hundreds), and we’re not going to complicate it further. I find it extremely useful. If it “shares the shit out of you” (whatever that means, again, I’m from Norway), then just ignore that it exists.

Then I will take this on as a documentation task. Thanks. :slightly_smiling_face:

2 Likes

On this area, I was puzzled for some times that when we use a return in a partial, all what is inside the partial is not effectively writen (html) or done (image process) after the return is done.
You have to choose between : doing something or returning something.

I’ll try to propose some text for precising this behaviour in documentation.

1 Like

I wish you all the best. I am afraid this will be very hard to explain in a way that makes sense to most people.

I appreciate your positive mindset. How about:

There can be only one (1) return statement.

1 Like

It is not the “only 1 statement” that bothers me, it is that it is not a statement in the sense of all other statements, i.e. it is apparently not controlled by surrounding if conditions. That goes against all programming conventions I know.

1 Like

Sure, but do you also understand the difference in cost between “2 hours of programming” and “100 hours of programming”. This feature would never have existed if we wanted to obey all of the common programming conventions. The current solution is pragmatic – it works and it’s very useful.

Using it is simple:

  1. Define the result variable.
  2. Assign some data to that result variable (in if conditions if needed).
  3. Return that result variable.