My Work Notes

27 Apr 2022

Hugo path.Join and urls.Parse examples

path.Join joins path elements into a single path, adding a separating slash if necessary. All empty strings are ignored.

{{ path.Join "partial" "news.html" }} → "partial/news.html"

urls.Parse parses a URL and returns a URL structure, where fields are accessible by their names through the dot notation. Example:

{{ $url := urls.Parse "https://www.example.com:80/path?version=1.0&tags=cat,dog#create" }}
{{ $url.Hostname }} → "www.example.com"
{{ $url.Scheme }} → "https"
{{ $url.Port }} → "80"
{{ $url.Path }} → "/path"
{{ $url.RawQuery }} → "version=1.0&tags=cat,dog"
{{ $url.Query }} → "map[version:[1.0] tags:[cat dog]]"
{{ $url.Fragment }} → "create"