urls.Encode
Returns the given string encoded so it can be safely placed inside a URL query.
The urls.Encode
function encodes a string so it can be safely placed inside a URL query.
Example
Consider this front matter:
---
location: Chicago IL
tags:
- pizza
- beer
- hot dogs
title: The World's Greatest City
---
+++
location = 'Chicago IL'
tags = ['pizza', 'beer', 'hot dogs']
title = "The World's Greatest City"
+++
{
"location": "Chicago IL",
"tags": [
"pizza",
"beer",
"hot dogs"
],
"title": "The World's Greatest City"
}
Use the urlencode
function to encode the location and tags for use in URLs:
<header>
<h1>{{ .Title }}</h1>
{{ with .Params.location }}
<div><a href="/locations/{{ . | urlencode }}">{{ . }}</a></div>
{{ end }}
{{ with .Params.tags }}
<ul>
{{ range . }}
<li>
<a href="/tags/{{ . | urlencode }}">{{ . }}</a>
</li>
{{ end }}
</ul>
{{ end }}
</header>
Hugo renders this to:
<header>
<h1>The World's Greatest City</h1>
<div><a href="/locations/Chicago%20IL">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%20dogs">hot dogs</a>
</li>
</ul>
</header>