2019-10-21 04:22:28 -04:00
|
|
|
---
|
2023-10-20 03:42:39 -04:00
|
|
|
title: urls.URLize
|
|
|
|
linkTitle: urlize
|
2019-10-21 04:22:28 -04:00
|
|
|
description: Takes a string, sanitizes it for usage in URLs, and converts spaces to hyphens.
|
|
|
|
categories: [functions]
|
2023-10-20 03:42:39 -04:00
|
|
|
keywords: []
|
2019-10-21 04:22:28 -04:00
|
|
|
menu:
|
|
|
|
docs:
|
2023-05-22 10:43:12 -04:00
|
|
|
parent: functions
|
2023-10-20 03:42:39 -04:00
|
|
|
function:
|
|
|
|
aliases: [urlize]
|
|
|
|
returnType: string
|
|
|
|
signatures: [urls.URLize INPUT]
|
|
|
|
relatedFunctions:
|
|
|
|
- urls.Anchorize
|
|
|
|
- urls.URLize
|
|
|
|
aliases: [/functions/urlize]
|
2019-10-21 04:22:28 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
The following examples pull from a content file with the following front matter:
|
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
{{< code-toggle file="content/blog/greatest-city.md" fm=true copy=false >}}
|
2019-10-21 04:22:28 -04:00
|
|
|
title = "The World's Greatest City"
|
|
|
|
location = "Chicago IL"
|
|
|
|
tags = ["pizza","beer","hot dogs"]
|
2023-05-22 10:43:12 -04:00
|
|
|
{{< /code-toggle >}}
|
2019-10-21 04:22:28 -04:00
|
|
|
|
|
|
|
The following might be used as a partial within a [single page template][singletemplate]:
|
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
{{< code file="layouts/partials/content-header.html" >}}
|
2019-10-21 04:22:28 -04:00
|
|
|
<header>
|
2023-08-07 04:35:12 -04:00
|
|
|
<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 }}
|
2019-10-21 04:22:28 -04:00
|
|
|
<ul>
|
2023-08-07 04:35:12 -04:00
|
|
|
{{ range .}}
|
|
|
|
<li>
|
|
|
|
<a href="/tags/{{ . | urlize }}">{{ . }}</a>
|
|
|
|
</li>
|
|
|
|
{{ end }}
|
2019-10-21 04:22:28 -04:00
|
|
|
</ul>
|
2023-08-07 04:35:12 -04:00
|
|
|
{{ end }}
|
2019-10-21 04:22:28 -04:00
|
|
|
</header>
|
|
|
|
{{< /code >}}
|
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
The preceding partial would then output to the rendered page as follows:
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
```html
|
2019-10-21 04:22:28 -04:00
|
|
|
<header>
|
2023-05-22 10:43:12 -04:00
|
|
|
<h1>The World'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>
|
2019-10-21 04:22:28 -04:00
|
|
|
</header>
|
2023-05-22 10:43:12 -04:00
|
|
|
```
|
2019-10-21 04:22:28 -04:00
|
|
|
|
|
|
|
[singletemplate]: /templates/single-page-templates/
|