urlize.md (1885B)
1 ---
2 title: urlize
3 # linktitle: urlize
4 description: Takes a string, sanitizes it for usage in URLs, and converts spaces to hyphens.
5 date: 2017-02-01
6 publishdate: 2017-02-01
7 lastmod: 2017-02-01
8 categories: [functions]
9 menu:
10 docs:
11 parent: "functions"
12 keywords: [urls,strings]
13 signature: ["urlize INPUT"]
14 hugoversion:
15 deprecated: false
16 workson: []
17 relatedfuncs: []
18 ---
19
20 The following examples pull from a content file with the following front matter:
21
22 {{< code file="content/blog/greatest-city.md" copy="false">}}
23 +++
24 title = "The World's Greatest City"
25 location = "Chicago IL"
26 tags = ["pizza","beer","hot dogs"]
27 +++
28 {{< /code >}}
29
30 The following might be used as a partial within a [single page template][singletemplate]:
31
32 {{< code file="layouts/partials/content-header.html" download="content-header.html" >}}
33 <header>
34 <h1>{{.Title}}</h1>
35 {{ with .Params.location }}
36 <div><a href="/locations/{{ . | urlize}}">{{.}}</a></div>
37 {{ end }}
38 <!-- Creates a list of tags for the content and links to each of their pages -->
39 {{ with .Params.tags }}
40 <ul>
41 {{range .}}
42 <li>
43 <a href="/tags/{{ . | urlize }}">{{ . }}</a>
44 </li>
45 {{end}}
46 </ul>
47 {{ end }}
48 </header>
49 {{< /code >}}
50
51 The preceding partial would then output to the rendered page as follows, assuming the page is being built with Hugo's default pretty URLs.
52
53 {{< output file="/blog/greatest-city/index.html" >}}
54 <header>
55 <h1>The World's Greatest City</h1>
56 <div><a href="/locations/chicago-il">Chicago IL</a></div>
57 <ul>
58 <li>
59 <a href="/tags/pizza">pizza</a>
60 </li>
61 <li>
62 <a href="/tags/beer">beer</a>
63 </li>
64 <li>
65 <a href="/tags/hot-dogs">hot dogs</a>
66 </li>
67 </ul>
68 </header>
69 {{< /output >}}
70
71
72
73 [singletemplate]: /templates/single-page-templates/