HUGO
Menu
GitHub 87548 stars Mastodon

urls.URLize

Returns the given string, sanitized for usage in a URL.

Syntax

urls.URLize INPUT

Returns

string

Alias

urlize

anchorizeurlize 函數相似:

  • 使用 anchorize 函數生成 HTML id 屬性值
  • 使用 urlize 函數清理字符串以用於 URL

例如:

{{ $s := "A B C" }}
{{ $s | anchorize }} → a-b-c
{{ $s | urlize }} → a-b-c

{{ $s := "a b   c" }}
{{ $s | anchorize }} → a-b---c
{{ $s | urlize }} → a-b-c

{{ $s := "< a, b, & c >" }}
{{ $s | anchorize }} → -a-b--c-
{{ $s | urlize }} → a-b-c

{{ $s := "main.go" }}
{{ $s | anchorize }} → maingo
{{ $s | urlize }} → main.go

{{ $s := "Hugö" }}
{{ $s | anchorize }} → hugö
{{ $s | urlize }} → hug%C3%B6
{{__hugo_ctx/}}



## Example

Use the `urlize` function to create a link to a [term page](g).

Consider this site configuration:




taxonomies:
  author: authors
[taxonomies]
  author = 'authors'
{
   "taxonomies": {
      "author": "authors"
   }
}
And this front matter:
---
authors:
- Victor Hugo
title: Les Misérables
---
+++
authors = ['Victor Hugo']
title = 'Les Misérables'
+++
{
   "authors": [
      "Victor Hugo"
   ],
   "title": "Les Misérables"
}
The published site will have this structure: ```text public/ ├── authors/ │ ├── victor-hugo/ │ │ └── index.html │ └── index.html ├── books/ │ ├── les-miserables/ │ │ └── index.html │ └── index.html └── index.html

To create a link to the term page:

{{ $taxonomy := "authors" }}
{{ $term := "Victor Hugo" }}
{{ with index .Site.Taxonomies $taxonomy (urlize $term) }}
  <a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a>
{{ end }}

To generate a list of term pages associated with a given content page, use the GetTerms method on a Page object.


Last updated: January 1, 0001
Improve this page