Files
hugo/content/functions/urlize.md
T
Bud Parr 7fd64f19c8 Merge bp/theme-design branch containing Bud Parr's new design
It's a beautiful thing. A thousand thanks, Bud.

See PR rdwatters/hugo-docs-concept#118
2017-05-26 14:25:22 -05:00

2.0 KiB

title, description, date, publishdate, lastmod, categories, menu, tags, godocref, ns, signature, hugoversion, deprecated, workson, relatedfuncs
title description date publishdate lastmod categories menu tags godocref ns signature hugoversion deprecated workson relatedfuncs
urlize Takes a string, sanitizes it for usage in URLs, and converts spaces to hyphens. 2017-02-01 2017-02-01 2017-02-01
functions
docs
parent
functions
urls
strings
urlize INPUT
false

urlize takes a string, sanitizes it for usage in URLs, and converts spaces to hyphens ("-").

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

{{% code file="content/blog/greatest-city.md" copy="false"%}}

+++
title = "The World's Greatest City"
location = "Chicago IL"
tags = ["pizza","beer","hot dogs"]
+++

{{% /code %}}

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

{{% code file="layouts/partials/content-header.html" download="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, assuming the page is being built with Hugo's default pretty URLs.

{{% output file="/blog/greatest-city/index.html" %}}

<header>
    <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>
</header>

{{% /output %}}