Best practices with building links in templates

What’s the recommended way to build a link in templates?

Examples:

{{$section := .Section}}
<a href="{{print ($section | relURL) "/" (.Title | urlize) "/"}}">
<a href="{{print "/products/" (.Title | urlize) "/"}}">
<a href="{{printf "/products/%s/" (.Title | urlize)}}">

<a href="/products/ {{(.Title | urlize)}}">

Thanks.

My question is geared towards links in layout templates (list.html, single.html etc), not content files. Sorry, should have been clearer in my initial post.

My mistake. Look at the ref and relref functions, not the shortcodes.

1 Like

I am curious about this too so I’m looking forward for answers.

I think the best is the most “portable”. If you change a folder name in content (section), for some of the options you have to go in the templates and make changes accordingly.

Looking forward to hear some opinions from the experts. :slight_smile:

Hugo is a content management system. Using the ref and relref functions you can refer to other pages by title (id) instead of by filename. This is very portable.

2 Likes