aboutsummaryrefslogtreecommitdiffhomepage
path: root/docs/content/en/functions/urls/URLize.md
blob: bf44a82d1ce5b16f1db3bd6b0a692011dc41508f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
---
title: urls.URLize
description: Returns the given string, sanitized for usage in a URL.
categories: []
keywords: []
action:
  aliases: [urlize]
  related:
    - functions/urls/Anchorize
  returnType: string
  signatures: [urls.URLize INPUT]
aliases: [/functions/urlize]
---

{{% include "/functions/urls/_common/anchorize-vs-urlize.md" %}}

## Example

Use the `urlize` function to create a link to a [term] page.

Consider this site configuration:

{{< code-toggle file=hugo >}}
[taxonomies]
author = 'authors'
{{< /code-toggle >}}

And this front matter:

{{< code-toggle file=content/books/les-miserables.md fm=true >}}
title = 'Les Misérables'
authors = ['Victor Hugo']
{{< /code-toggle >}}

The published site will have this structure:

```text
public/
├── authors/
│   ├── victor-hugo/
│   │   └── index.html
│   └── index.html
├── books/
│   ├── les-miserables/
│   │   └── index.html
│   └── index.html
└── index.html
```

To create a link to the term page:

```go-html-template
{{ $taxonomy := "authors" }}
{{ $term := "Victor Hugo" }}
{{ with index .Site.Taxonomies $taxonomy (urlize $term) }}
  <a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a>
{{ end }}
```

To generate a list of term pages associated with a given content page, use the [`GetTerms`] method on a `Page` object.

[`GetTerms`]: /methods/page/getterms/
[term]: /getting-started/glossary/#term