urls.URLize
语法
urls.URLize INPUT
返回
string
别名
urlize
- 使用
anchorize
函数生成 HTMLid
属性值 - 使用
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
示例
使用 urlize
函数创建一个指向术语页面的链接。
考虑以下站点配置
hugo.
taxonomies:
author: authors
[taxonomies]
author = 'authors'
{
"taxonomies": {
"author": "authors"
}
}
以及以下前言
content/books/les-miserables.md
---
authors:
- Victor Hugo
title: Les Misérables
---
+++
authors = ['Victor Hugo']
title = 'Les Misérables'
+++
{
"authors": [
"Victor Hugo"
],
"title": "Les Misérables"
}
发布的站点将具有以下结构
public/
├── authors/
│ ├── victor-hugo/
│ │ └── index.html
│ └── index.html
├── books/
│ ├── les-miserables/
│ │ └── index.html
│ └── index.html
└── index.html
创建指向术语页面的链接
{{ $taxonomy := "authors" }}
{{ $term := "Victor Hugo" }}
{{ with index .Site.Taxonomies $taxonomy (urlize $term) }}
<a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a>
{{ end }}
要生成与给定内容页面关联的术语页面列表,请在 Page
对象上使用 GetTerms
方法。