Partial for checking min/max Hugo version

The need to take actions based on the version Hugo that is in use has come up a few times in my Hugo meanderings, so I came up with a partial:

{{- $ctx := . -}}
{{- $verInRange := true -}}
{{- $hugoVersion := split hugo.Version "." -}}
{{- $verMajor := int (index $hugoVersion 0) -}}
{{- $verMinor := int (index $hugoVersion 1) -}}
{{- $verPatch := int (index $hugoVersion 2) -}}
{{- $minMajor := int (.minMajor | default 0) -}}
{{- $minMinor := int (.minMinor | default $verMinor) -}}
{{- $minPatch := int (.minPatch | default 0) -}}
{{- $maxMajor := int (.maxMajor | default $verMajor) -}}
{{- $maxMinor := int (.maxMinor | default $verMinor) -}}
{{- $maxPatch := int (.maxPatch | default $verPatch) -}}
{{- if lt $verMajor $minMajor -}}
	{{- $verInRange = false -}}
{{- else if and (eq $verMajor $minMajor) (lt $verMinor $minMinor) -}}
	{{- $verInRange = false -}}
{{- else if and (eq $verMajor $minMajor) (eq $verMinor $minMinor) (lt $verPatch $minPatch) -}}
	{{- $verInRange = false -}}
{{- end -}}
{{- if $verInRange -}}
	{{- if gt $verMajor $maxMajor -}}
		{{- $verInRange = false -}}
	{{- else if and (eq $verMajor $maxMajor) (gt $verMinor $maxMinor) -}}
		{{- $verInRange = false -}}
	{{- else if and (eq $verMajor $maxMajor) (eq $verMinor $maxMinor) (gt $verPatch $maxPatch) -}}
		{{- $verInRange = false -}}
	{{- end -}}
{{- end -}}
{{- return $verInRange -}}

which can be called with something like

{{- if partial "check-hugo-version.html" (dict "minMajor" 0 "minMinor" 101 "minPatch" 0) -}}
   ...do stuff that needs at least 0.101.0...
{{- else -}}
   ...older stuff...
{{- end -}}

I also have unit tests at

combined with

Hope some of you find this useful.

7 Likes

Apparently this was totally unnecessary because bep already has code that handles this without any additional functions required.

Just do

{{ if hugo.Version ge "0.101.0" }}
   Something that depends on at least 0.101.0
{{ else }}
   Fallback code and/or warn user that they need a newer version
{{ end }}
1 Like