Skip to content

Commit 11977b9

Browse files
authored
Make relURL and related functions consistent (#1895)
This addresses changes made in: gohugoio/hugo#10002 Closes #469
1 parent f121802 commit 11977b9

File tree

4 files changed

+184
-94
lines changed

4 files changed

+184
-94
lines changed

content/en/functions/abslangurl.md

+49-15
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,61 @@
11
---
22
title: absLangURL
3-
description: Adds the absolute URL with correct language prefix according to site configuration for multilingual.
4-
date: 2017-02-01
5-
publishdate: 2017-02-01
6-
lastmod: 2017-02-01
3+
description: Returns an absolute URL with a language prefix, if any.
74
categories: [functions]
85
menu:
96
docs:
10-
parent: "functions"
11-
keywords: [multilingual,i18n,urls]
7+
parent: functions
8+
keywords: [urls, multilingual,i18n]
129
signature: ["absLangURL INPUT"]
13-
workson: []
14-
hugoversion:
15-
relatedfuncs: [relLangURL]
16-
deprecated: false
17-
aliases: []
1810
---
1911

20-
Both `absLangURL` and [`relLangURL`](/functions/rellangurl/) are similar to their [`absURL`](/functions/absurl/) and [`relURL`](/functions/relurl) relatives but will add the correct language prefix when the site is configured with more than one language.
12+
Use this function with both monolingual and multilingual configurations. The URL returned by this function depends on:
2113

22-
So for a site `baseURL` set to `https://example.com/hugo/` and the current language is `en`:
14+
- Whether the input begins with a slash
15+
- The `baseURL` in site configuration
16+
- The language prefix, if any
2317

18+
In examples that follow, the project is multilingual with content in both Español (`es`) and English (`en`). The default language is Español. The returned values are from the English site.
19+
20+
### Input does not begin with a slash
21+
22+
If the input does not begin with a slash, the resulting URL will be correct regardless of the `baseURL`.
23+
24+
With `baseURL = https://example.org/`
25+
26+
```go-html-template
27+
{{ absLangURL "" }} → https://example.org/en/
28+
{{ absLangURL "articles" }} → https://example.org/en/articles
29+
{{ absLangURL "style.css" }} → https://example.org/en/style.css
30+
```
31+
32+
With `baseURL = https://example.org/docs/`
33+
34+
```go-html-template
35+
{{ absLangURL "" }} → https://example.org/docs/en/
36+
{{ absLangURL "articles" }} → https://example.org/docs/en/articles
37+
{{ absLangURL "style.css" }} → https://example.org/docs/en/style.css
2438
```
25-
{{ "blog/" | absLangURL }} → "https://example.com/hugo/en/blog/"
26-
{{ "blog/" | relLangURL }} → "/hugo/en/blog/"
39+
40+
### Input begins with a slash
41+
42+
If the input begins with a slash, the resulting URL will be incorrect when the `baseURL` includes a subdirectory. With a leading slash, the function returns a URL relative to the protocol+host section of the `baseURL`.
43+
44+
With `baseURL = https://example.org/`
45+
46+
```go-html-template
47+
{{ absLangURL "/" }} → https://example.org/en/
48+
{{ absLangURL "/articles" }} → https://example.org/en/articles
49+
{{ absLangURL "/style.css" }} → https://example.org/en/style.css
2750
```
51+
52+
With `baseURL = https://example.org/docs/`
53+
54+
```go-html-template
55+
{{ absLangURL "/" }} → https://example.org/en/
56+
{{ absLangURL "/articles" }} → https://example.org/en/articles
57+
{{ absLangURL "/style.css" }} → https://example.org/en/style.css
58+
59+
{{% note %}}
60+
The last three examples are not desirable in most situations. As a best practice, never include a leading slash when using this function.
61+
{{% /note %}}

content/en/functions/absurl.md

+43-32
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,61 @@
11
---
22
title: absURL
3-
description: Creates an absolute URL based on the configured baseURL.
4-
date: 2017-02-01
5-
publishdate: 2017-02-01
6-
lastmod: 2017-02-01
3+
description: Returns an absolute URL.
74
categories: [functions]
85
menu:
96
docs:
10-
parent: "functions"
7+
parent: functions
118
keywords: [urls]
129
signature: ["absURL INPUT"]
13-
workson: []
14-
hugoversion:
15-
relatedfuncs: [relURL]
16-
deprecated: false
17-
aliases: []
1810
---
1911

20-
Both `absURL` and `relURL` consider the configured value of `baseURL` in your site's [`config` file][configuration]. Given a `baseURL` set to `https://example.com/hugo/`:
12+
With multilingual configurations, use the [`absLangURL`] function instead. The URL returned by this function depends on:
2113

14+
- Whether the input begins with a slash
15+
- The `baseURL` in site configuration
16+
17+
### Input does not begin with a slash
18+
19+
If the input does not begin with a slash, the resulting URL will be correct regardless of the `baseURL`.
20+
21+
With `baseURL = https://example.org/`
22+
23+
```go-html-template
24+
{{ absURL "" }} → https://example.org/
25+
{{ absURL "articles" }} → https://example.org/articles
26+
{{ absURL "style.css" }} → https://example.org/style.css
2227
```
23-
{{ "mystyle.css" | absURL }} → "https://example.com/hugo/mystyle.css"
24-
{{ "mystyle.css" | relURL }} → "/hugo/mystyle.css"
25-
{{ "http://gohugo.io/" | relURL }} → "http://gohugo.io/"
26-
{{ "http://gohugo.io/" | absURL }} → "http://gohugo.io/"
28+
29+
With `baseURL = https://example.org/docs/`
30+
31+
```go-html-template
32+
{{ absURL "" }} → https://example.org/docs/
33+
{{ absURL "articles" }} → https://example.org/docs/articles
34+
{{ absURL "style.css" }} → https://example.org/docs/style.css
2735
```
2836

29-
The last two examples may look strange but can be very useful. For example, the following shows how to use `absURL` in [JSON-LD structured data (SEO)][jsonld], where some of your images for a piece of content may or may not be hosted locally:
37+
### Input begins with a slash
3038

31-
{{< code file="layouts/partials/schemaorg-metadata.html" download="schemaorg-metadata.html" >}}
32-
<script type="application/ld+json">
33-
{
34-
"@context" : "http://schema.org",
35-
"@type" : "BlogPosting",
36-
"image" : {{ apply .Params.images "absURL" "." }}
37-
}
38-
</script>
39-
{{< /code >}}
39+
If the input begins with a slash, the resulting URL will be incorrect when the `baseURL` includes a subdirectory. With a leading slash, the function returns a URL relative to the protocol+host section of the `baseURL`.
4040

41-
The above uses the [apply function][] and also exposes how the Go template parser JSON-encodes objects inside `<script>` tags. See [the safeJS template function][safejs] for examples of how to tell Hugo not to escape strings inside such tags.
41+
With `baseURL = https://example.org/`
42+
43+
```go-html-template
44+
{{ absURL "/" }} → https://example.org/
45+
{{ absURL "/articles" }} → https://example.org/articles
46+
{{ absURL "/style.css" }} → https://example.org/style.css
47+
```
48+
49+
With `baseURL = https://example.org/docs/`
50+
51+
```go-html-template
52+
{{ absURL "/" }} → https://example.org/
53+
{{ absURL "/articles" }} → https://example.org/articles
54+
{{ absURL "/style.css" }} → https://example.org/style.css
55+
```
4256

43-
{{% note "Ending Slash" %}}
44-
`absURL` and `relURL` are smart about missing slashes, but they will *not* add a closing slash to a URL if it is not present.
57+
{{% note %}}
58+
The last three examples are not desirable in most situations. As a best practice, never include a leading slash when using this function.
4559
{{% /note %}}
4660

47-
[apply function]: /functions/apply/
48-
[configuration]: /getting-started/configuration/
49-
[jsonld]: https://developers.google.com/search/docs/guides/intro-structured-data
50-
[safejs]: /functions/safejs
61+
[`absLangURL`]: /functions/abslangurl/

content/en/functions/relLangURL.md

+49-16
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,62 @@
11
---
22
title: relLangURL
3-
description: Adds the relative URL with correct language prefix according to site configuration for multilingual.
4-
date: 2017-02-01
5-
publishdate: 2017-02-01
6-
lastmod: 2017-02-01
7-
keywords: [multilingual,i18n,urls]
3+
description: Returns a relative URL with a language prefix, if any.
84
categories: [functions]
95
menu:
106
docs:
11-
parent: "functions"
7+
parent: functions
8+
keywords: [urls, multilingual,i18n]
129
signature: ["relLangURL INPUT"]
13-
workson: []
14-
hugoversion:
15-
relatedfuncs: []
16-
deprecated: false
17-
aliases: []
1810
---
1911

20-
`absLangURL` and `relLangURL` functions are similar to their [`absURL`](/functions/absurl/) and [`relURL`](/functions/relurl/) relatives but will add the correct language prefix when the site is configured with more than one language. (See [Configure Languages][multiliconfig].)
12+
Use this function with both monolingual and multilingual configurations. The URL returned by this function depends on:
2113

22-
So for a site `baseURL` set to `https://example.com/hugo/` and the current language is `en`:
14+
- Whether the input begins with a slash
15+
- The `baseURL` in site configuration
16+
- The language prefix, if any
2317

18+
In examples that follow, the project is multilingual with content in both Español (`es`) and English (`en`). The default language is Español. The returned values are from the English site.
19+
20+
### Input does not begin with a slash
21+
22+
If the input does not begin with a slash, the resulting URL will be correct regardless of the `baseURL`.
23+
24+
With `baseURL = https://example.org/`
25+
26+
```go-html-template
27+
{{ relLangURL "" }} → /en/
28+
{{ relLangURL "articles" }} → /en/articles
29+
{{ relLangURL "style.css" }} → /en/style.css
30+
```
31+
32+
With `baseURL = https://example.org/docs/`
33+
34+
```go-html-template
35+
{{ relLangURL "" }} → /docs/en/
36+
{{ relLangURL "articles" }} → /docs/en/articles
37+
{{ relLangURL "style.css" }} → /docs/en/style.css
2438
```
25-
{{ "blog/" | absLangURL }} → "https://example.com/hugo/en/blog/"
26-
{{ "blog/" | relLangURL }} → "/hugo/en/blog/"
39+
40+
### Input begins with a slash
41+
42+
If the input begins with a slash, the resulting URL will be incorrect when the `baseURL` includes a subdirectory. With a leading slash, the function returns a URL relative to the protocol+host section of the `baseURL`.
43+
44+
With `baseURL = https://example.org/`
45+
46+
```go-html-template
47+
{{ relLangURL "/" }} → /en/
48+
{{ relLangURL "/articles" }} → /en/articles
49+
{{ relLangURL "/style.css" }} → /en/style.css
50+
```
51+
52+
With `baseURL = https://example.org/docs/`
53+
54+
```go-html-template
55+
{{ relLangURL "/" }} → /en/
56+
{{ relLangURL "/articles" }} → /en/articles
57+
{{ relLangURL "/style.css" }} → /en/style.css
2758
```
2859

29-
[multiliconfig]: /content-management/multilingual/#configure-languages
60+
{{% note %}}
61+
The last three examples are not desirable in most situations. As a best practice, never include a leading slash when using this function.
62+
{{% /note %}}

content/en/functions/relurl.md

+43-31
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,61 @@
11
---
22
title: relURL
3-
description: Creates a baseURL-relative URL.
4-
date: 2017-02-01
5-
publishdate: 2017-02-01
3+
description: Returns a relative URL.
64
categories: [functions]
75
menu:
86
docs:
9-
parent: "functions"
7+
parent: functions
108
keywords: [urls]
119
signature: ["relURL INPUT"]
12-
workson: []
13-
hugoversion:
14-
relatedfuncs: [absURL]
15-
deprecated: false
16-
aliases: []
1710
---
1811

19-
Both `absURL` and `relURL` consider the configured value of `baseURL` in your site's [`config` file][configuration]. Given a `baseURL` set to `https://example.com/hugo/`:
12+
With multilingual configurations, use the [`relLangURL`] function instead. The URL returned by this function depends on:
2013

14+
- Whether the input begins with a slash
15+
- The `baseURL` in site configuration
16+
17+
### Input does not begin with a slash
18+
19+
If the input does not begin with a slash, the resulting URL will be correct regardless of the `baseURL`.
20+
21+
With `baseURL = https://example.org/`
22+
23+
```go-html-template
24+
{{ relURL "" }} → /
25+
{{ relURL "articles" }} → /articles
26+
{{ relURL "style.css" }} → /style.css
2127
```
22-
{{ "mystyle.css" | absURL }} → "https://example.com/hugo/mystyle.css"
23-
{{ "mystyle.css" | relURL }} → "/hugo/mystyle.css"
24-
{{ "http://gohugo.io/" | relURL }} → "http://gohugo.io/"
25-
{{ "http://gohugo.io/" | absURL }} → "http://gohugo.io/"
28+
29+
With `baseURL = https://example.org/docs/`
30+
31+
```go-html-template
32+
{{ relURL "" }} → /docs/
33+
{{ relURL "articles" }} → /docs/articles
34+
{{ relURL "style.css" }} → /docs/style.css
2635
```
2736

28-
The last two examples may look strange but can be very useful. For example, the following shows how to use `absURL` in [JSON-LD structured data for SEO][jsonld] where some of your images for a piece of content may or may not be hosted locally:
37+
### Input begins with a slash
2938

30-
{{< code file="layouts/partials/schemaorg-metadata.html" download="schemaorg-metadata.html" >}}
31-
<script type="application/ld+json">
32-
{
33-
"@context" : "https://schema.org",
34-
"@type" : "BlogPosting",
35-
"image" : {{ apply .Params.images "absURL" "." }}
36-
}
37-
</script>
38-
{{< /code >}}
39+
If the input begins with a slash, the resulting URL will be incorrect when the `baseURL` includes a subdirectory. With a leading slash, the function returns a URL relative to the protocol+host section of the `baseURL`.
3940

40-
The above uses the [apply function][] and also exposes how the Go template parser JSON-encodes objects inside `<script>` tags. See [the safeJS template function][safejs] for examples of how to tell Hugo not to escape strings inside of such tags.
41+
With `baseURL = https://example.org/`
42+
43+
```go-html-template
44+
{{ relURL "/" }} → /
45+
{{ relURL "/articles" }} → /articles
46+
{{ relURL "style.css" }} → /style.css
47+
```
48+
49+
With `baseURL = https://example.org/docs/`
50+
51+
```go-html-template
52+
{{ relURL "/" }} → /
53+
{{ relURL "/articles" }} → /articles
54+
{{ relURL "/style.css" }} → /style.css
55+
```
4156

42-
{{% note "Ending Slash" %}}
43-
`absURL` and `relURL` are smart about missing slashes, but they will *not* add a closing slash to a URL if it is not present.
57+
{{% note %}}
58+
The last three examples are not desirable in most situations. As a best practice, never include a leading slash when using this function.
4459
{{% /note %}}
4560

46-
[apply function]: /functions/apply/
47-
[configuration]: /getting-started/configuration/
48-
[jsonld]: https://developers.google.com/search/docs/advanced/structured-data/intro-structured-data
49-
[safejs]: /functions/safejs
61+
[`relLangURL`]: /functions/rellangurl/

0 commit comments

Comments
 (0)