summaryrefslogtreecommitdiffstats
path: root/docs/content/en/functions/urls/URLize.md
blob: 3c80a92f80727ef41a70ac2971b42f88f8e5733e (plain) (blame)
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
64
65
66
67
68
69
---
title: urls.URLize
linkTitle: urlize
description: Takes a string, sanitizes it for usage in URLs, and converts spaces to hyphens.
categories: [functions]
keywords: []
menu:
  docs:
    parent: functions
function:
  aliases: [urlize]
  returnType: string
  signatures: [urls.URLize INPUT]
relatedFunctions:
  - urls.Anchorize
  - urls.URLize
aliases: [/functions/urlize]
---

The following examples pull from a content file with the following front matter:

{{< code-toggle file="content/blog/greatest-city.md" fm=true copy=false >}}
title = "The World's Greatest City"
location = "Chicago IL"
tags = ["pizza","beer","hot dogs"]
{{< /code-toggle >}}

The following might be used as a partial within a [single page template][singletemplate]:

{{< code file="layouts/partials/content-header.html" >}}
<header>
  <h1>{{ .Title }}</h1>
  {{ with .Params.location }}
    <div><a href="/locations/{{ . | urlize }}">{{ . }}</a></div>
  {{ end }}
  <!-- Creates a list of tags for the content and links to each of their pages -->
  {{ with .Params.tags }}
    <ul>
      {{ range .}}
        <li>
          <a href="/tags/{{ . | urlize }}">{{ . }}</a>
        </li>
      {{ end }}
    </ul>
  {{ end }}
</header>
{{< /code >}}

The preceding partial would then output to the rendered page as follows:

```html
<header>
  <h1>The World&#39;s Greatest City</h1>
  <div><a href="/locations/chicago-il">Chicago IL</a></div>
  <ul>
    <li>
      <a href="/tags/pizza">pizza</a>
    </li>
    <li>
      <a href="/tags/beer">beer</a>
    </li>
    <li>
      <a href="/tags/hot-dogs">hot dogs</a>
    </li>
  </ul>
</header>
```

[singletemplate]: /templates/single-page-templates/