Return from within render-hook

Hi, it’s writtten that “return”, without return value, within a template exists it. I try to do that within render-link.html with this:

{{- if $isRemote -}}
	{{ if not .Destination}}{{return}}{{end}}
{{- else }}

but it’s ignored, I get the homepage (for the given language). I would like links with no destination (meaning, with destination or translation to be added later) to not be processed as a link.


{{- $page := "" -}}
{{- $glossaireentry := false -}}
{{- $destination := .Destination -}}
{{- $isRemote := or (strings.Contains $destination "://") (strings.HasPrefix $destination "www") -}}
{{- if $isRemote -}}
	{{- if not (strings.HasPrefix $destination "http") }}
		{{- $destination = print "https://" $destination -}}
	{{- end -}}
	{{ if eq .Destination ""}}{{return}}{{end}}
{{- else }}
	{{- $url := urls.Parse .Destination }}
	{{- $page = site.GetPage $url.Path }}
	{{- if $page }}
		{{- $destination = $page.RelPermalink }}
	{{- else -}}
		{{ with resources.Get .Destination }}
			{{ $destination = .RelPermalink}}
		{{ end}}
	{{- end }}
{{ end }}[27;5;9~]
<a href="{{ $destination }}" {{if $isRemote}}rel="nofollow external" target="blank" {{else}}onclick="document.querySelector('iframe').removeAttribute('hidden');" target="iframe_glossary"{{end}}>{{.Text}}</a>

Used within partial templates, terminates template execution and returns the given value, if any.

yeah, I thought so. Is there a reason behind this limitation ? Would something fail if that was extended to all templates ?

Where do you return the value in, say, a render hook? Who receives it?

Well one could allow only instances without operand ?

I don’t understand what you mean, but your example would not work, even in a partial:

{{- if $isRemote -}}
	{{ if not .Destination}}{{return}}{{end}}
{{- else }}

Ref. the docs:

A partial that returns a value must contain only one return statement, placed at the end of the template.

Note that the return keyword is not a native Go template “thing”, this is Hugo doing template rewrites on the AST level, and that would not make sense/be worth it for all templates.

I have read about a “native Go template keyword”, but I’m not sure what’s the status of that keyword.

ah ok, I treated it as an “exit” statement but it really doesn’t work that way.
I’ll change the code’s logic. Unless there is another way to quit the render hook abruptly ?

Based on this comment from Rob Pike over a year ago…

And for the record, I still don’t want this, but the proposal has been accepted so my dislike is not sufficient to stop it.

… I’d say it’s unlikely to be implemented any time soon.

Rob Pike has done many great things for that project, but he has been overly grumpy about accepting changes in “his” package. For many years there were obvious data races that he refused to fix because it meant rewriting the parser – he wanted to keep the original implementation because it was linked to from a popular presentation he did on parsers (it’s on YouTube).

I suspect the return keyword will happen an hour after Russ Cox decides that he needs/wants it.