Parse a URL in Hugo

Published: Jan 25, 2019
Updated: Jan 14, 2022

Need to parse a URL in your Hugo templates? Thanks to the urls.Parse function, you don’t have to do a bunch of string manipulation. Here’s a quick example.

We’ll start with this code:

{{ $url := "" }}
{{ $url = urls.Parse $url }}

The urls.Parse function returns a URL struct, so the value of each struct field would be:

Field Value
$url.Scheme https
$url.Path /make-a-hugo-blog-from-scratch/
$url.Fragment wrap-up