mirror of
https://github.com/tiennm99/hugoDocs.git
synced 2026-10-14 04:16:13 +00:00
Make relURL and related functions consistent (#1895)
This addresses changes made in: https://github.com/gohugoio/hugo/pull/10002 Closes #469
This commit is contained in:
1 parent
f12180207a
commit
11977b96f3
4 files changed
+187
-97
No files matched your search
@@ -1,27 +1,61 @@
|
|||||||
---
|
---
|
||||||
title: absLangURL
|
title: absLangURL
|
||||||
description: Adds the absolute URL with correct language prefix according to site configuration for multilingual.
|
description: Returns an absolute URL with a language prefix, if any.
|
||||||
date: 2017-02-01
|
|
||||||
publishdate: 2017-02-01
|
|
||||||
lastmod: 2017-02-01
|
|
||||||
categories: [functions]
|
categories: [functions]
|
||||||
menu:
|
menu:
|
||||||
docs:
|
docs:
|
||||||
parent: "functions"
|
parent: functions
|
||||||
keywords: [multilingual,i18n,urls]
|
keywords: [urls, multilingual,i18n]
|
||||||
signature: ["absLangURL INPUT"]
|
signature: ["absLangURL INPUT"]
|
||||||
workson: []
|
|
||||||
hugoversion:
|
|
||||||
relatedfuncs: [relLangURL]
|
|
||||||
deprecated: false
|
|
||||||
aliases: []
|
|
||||||
---
|
---
|
||||||
|
|
||||||
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.
|
Use this function with both monolingual and multilingual configurations. The URL returned by this function depends on:
|
||||||
|
|
||||||
So for a site `baseURL` set to `https://example.com/hugo/` and the current language is `en`:
|
- Whether the input begins with a slash
|
||||||
|
- The `baseURL` in site configuration
|
||||||
|
- The language prefix, if any
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
### Input does not begin with a slash
|
||||||
|
|
||||||
|
If the input does not begin with a slash, the resulting URL will be correct regardless of the `baseURL`.
|
||||||
|
|
||||||
|
With `baseURL = https://example.org/`
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ absLangURL "" }} → https://example.org/en/
|
||||||
|
{{ absLangURL "articles" }} → https://example.org/en/articles
|
||||||
|
{{ absLangURL "style.css" }} → https://example.org/en/style.css
|
||||||
```
|
```
|
||||||
{{ "blog/" | absLangURL }} → "https://example.com/hugo/en/blog/"
|
|
||||||
{{ "blog/" | relLangURL }} → "/hugo/en/blog/"
|
With `baseURL = https://example.org/docs/`
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ absLangURL "" }} → https://example.org/docs/en/
|
||||||
|
{{ absLangURL "articles" }} → https://example.org/docs/en/articles
|
||||||
|
{{ absLangURL "style.css" }} → https://example.org/docs/en/style.css
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Input begins with a slash
|
||||||
|
|
||||||
|
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`.
|
||||||
|
|
||||||
|
With `baseURL = https://example.org/`
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ absLangURL "/" }} → https://example.org/en/
|
||||||
|
{{ absLangURL "/articles" }} → https://example.org/en/articles
|
||||||
|
{{ absLangURL "/style.css" }} → https://example.org/en/style.css
|
||||||
|
```
|
||||||
|
|
||||||
|
With `baseURL = https://example.org/docs/`
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ absLangURL "/" }} → https://example.org/en/
|
||||||
|
{{ absLangURL "/articles" }} → https://example.org/en/articles
|
||||||
|
{{ absLangURL "/style.css" }} → https://example.org/en/style.css
|
||||||
|
|
||||||
|
{{% note %}}
|
||||||
|
The last three examples are not desirable in most situations. As a best practice, never include a leading slash when using this function.
|
||||||
|
{{% /note %}}
|
||||||
@@ -1,50 +1,61 @@
|
|||||||
---
|
---
|
||||||
title: absURL
|
title: absURL
|
||||||
description: Creates an absolute URL based on the configured baseURL.
|
description: Returns an absolute URL.
|
||||||
date: 2017-02-01
|
|
||||||
publishdate: 2017-02-01
|
|
||||||
lastmod: 2017-02-01
|
|
||||||
categories: [functions]
|
categories: [functions]
|
||||||
menu:
|
menu:
|
||||||
docs:
|
docs:
|
||||||
parent: "functions"
|
parent: functions
|
||||||
keywords: [urls]
|
keywords: [urls]
|
||||||
signature: ["absURL INPUT"]
|
signature: ["absURL INPUT"]
|
||||||
workson: []
|
|
||||||
hugoversion:
|
|
||||||
relatedfuncs: [relURL]
|
|
||||||
deprecated: false
|
|
||||||
aliases: []
|
|
||||||
---
|
---
|
||||||
|
|
||||||
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/`:
|
With multilingual configurations, use the [`absLangURL`] function instead. The URL returned by this function depends on:
|
||||||
|
|
||||||
```
|
- Whether the input begins with a slash
|
||||||
{{ "mystyle.css" | absURL }} → "https://example.com/hugo/mystyle.css"
|
- The `baseURL` in site configuration
|
||||||
{{ "mystyle.css" | relURL }} → "/hugo/mystyle.css"
|
|
||||||
{{ "http://gohugo.io/" | relURL }} → "http://gohugo.io/"
|
### Input does not begin with a slash
|
||||||
{{ "http://gohugo.io/" | absURL }} → "http://gohugo.io/"
|
|
||||||
|
If the input does not begin with a slash, the resulting URL will be correct regardless of the `baseURL`.
|
||||||
|
|
||||||
|
With `baseURL = https://example.org/`
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ absURL "" }} → https://example.org/
|
||||||
|
{{ absURL "articles" }} → https://example.org/articles
|
||||||
|
{{ absURL "style.css" }} → https://example.org/style.css
|
||||||
```
|
```
|
||||||
|
|
||||||
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:
|
With `baseURL = https://example.org/docs/`
|
||||||
|
|
||||||
{{< code file="layouts/partials/schemaorg-metadata.html" download="schemaorg-metadata.html" >}}
|
```go-html-template
|
||||||
<script type="application/ld+json">
|
{{ absURL "" }} → https://example.org/docs/
|
||||||
{
|
{{ absURL "articles" }} → https://example.org/docs/articles
|
||||||
"@context" : "http://schema.org",
|
{{ absURL "style.css" }} → https://example.org/docs/style.css
|
||||||
"@type" : "BlogPosting",
|
```
|
||||||
"image" : {{ apply .Params.images "absURL" "." }}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
{{< /code >}}
|
|
||||||
|
|
||||||
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.
|
### Input begins with a slash
|
||||||
|
|
||||||
{{% note "Ending Slash" %}}
|
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`.
|
||||||
`absURL` and `relURL` are smart about missing slashes, but they will *not* add a closing slash to a URL if it is not present.
|
|
||||||
|
With `baseURL = https://example.org/`
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ absURL "/" }} → https://example.org/
|
||||||
|
{{ absURL "/articles" }} → https://example.org/articles
|
||||||
|
{{ absURL "/style.css" }} → https://example.org/style.css
|
||||||
|
```
|
||||||
|
|
||||||
|
With `baseURL = https://example.org/docs/`
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ absURL "/" }} → https://example.org/
|
||||||
|
{{ absURL "/articles" }} → https://example.org/articles
|
||||||
|
{{ absURL "/style.css" }} → https://example.org/style.css
|
||||||
|
```
|
||||||
|
|
||||||
|
{{% note %}}
|
||||||
|
The last three examples are not desirable in most situations. As a best practice, never include a leading slash when using this function.
|
||||||
{{% /note %}}
|
{{% /note %}}
|
||||||
|
|
||||||
[apply function]: /functions/apply/
|
[`absLangURL`]: /functions/abslangurl/
|
||||||
[configuration]: /getting-started/configuration/
|
|
||||||
[jsonld]: https://developers.google.com/search/docs/guides/intro-structured-data
|
|
||||||
[safejs]: /functions/safejs
|
|
||||||
@@ -1,29 +1,62 @@
|
|||||||
---
|
---
|
||||||
title: relLangURL
|
title: relLangURL
|
||||||
description: Adds the relative URL with correct language prefix according to site configuration for multilingual.
|
description: Returns a relative URL with a language prefix, if any.
|
||||||
date: 2017-02-01
|
|
||||||
publishdate: 2017-02-01
|
|
||||||
lastmod: 2017-02-01
|
|
||||||
keywords: [multilingual,i18n,urls]
|
|
||||||
categories: [functions]
|
categories: [functions]
|
||||||
menu:
|
menu:
|
||||||
docs:
|
docs:
|
||||||
parent: "functions"
|
parent: functions
|
||||||
|
keywords: [urls, multilingual,i18n]
|
||||||
signature: ["relLangURL INPUT"]
|
signature: ["relLangURL INPUT"]
|
||||||
workson: []
|
|
||||||
hugoversion:
|
|
||||||
relatedfuncs: []
|
|
||||||
deprecated: false
|
|
||||||
aliases: []
|
|
||||||
---
|
---
|
||||||
|
|
||||||
`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].)
|
Use this function with both monolingual and multilingual configurations. The URL returned by this function depends on:
|
||||||
|
|
||||||
So for a site `baseURL` set to `https://example.com/hugo/` and the current language is `en`:
|
- Whether the input begins with a slash
|
||||||
|
- The `baseURL` in site configuration
|
||||||
|
- The language prefix, if any
|
||||||
|
|
||||||
```
|
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.
|
||||||
{{ "blog/" | absLangURL }} → "https://example.com/hugo/en/blog/"
|
|
||||||
{{ "blog/" | relLangURL }} → "/hugo/en/blog/"
|
### Input does not begin with a slash
|
||||||
|
|
||||||
|
If the input does not begin with a slash, the resulting URL will be correct regardless of the `baseURL`.
|
||||||
|
|
||||||
|
With `baseURL = https://example.org/`
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ relLangURL "" }} → /en/
|
||||||
|
{{ relLangURL "articles" }} → /en/articles
|
||||||
|
{{ relLangURL "style.css" }} → /en/style.css
|
||||||
|
```
|
||||||
|
|
||||||
|
With `baseURL = https://example.org/docs/`
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ relLangURL "" }} → /docs/en/
|
||||||
|
{{ relLangURL "articles" }} → /docs/en/articles
|
||||||
|
{{ relLangURL "style.css" }} → /docs/en/style.css
|
||||||
```
|
```
|
||||||
|
|
||||||
[multiliconfig]: /content-management/multilingual/#configure-languages
|
### Input begins with a slash
|
||||||
|
|
||||||
|
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`.
|
||||||
|
|
||||||
|
With `baseURL = https://example.org/`
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ relLangURL "/" }} → /en/
|
||||||
|
{{ relLangURL "/articles" }} → /en/articles
|
||||||
|
{{ relLangURL "/style.css" }} → /en/style.css
|
||||||
|
```
|
||||||
|
|
||||||
|
With `baseURL = https://example.org/docs/`
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ relLangURL "/" }} → /en/
|
||||||
|
{{ relLangURL "/articles" }} → /en/articles
|
||||||
|
{{ relLangURL "/style.css" }} → /en/style.css
|
||||||
|
```
|
||||||
|
|
||||||
|
{{% note %}}
|
||||||
|
The last three examples are not desirable in most situations. As a best practice, never include a leading slash when using this function.
|
||||||
|
{{% /note %}}
|
||||||
@@ -1,49 +1,61 @@
|
|||||||
---
|
---
|
||||||
title: relURL
|
title: relURL
|
||||||
description: Creates a baseURL-relative URL.
|
description: Returns a relative URL.
|
||||||
date: 2017-02-01
|
|
||||||
publishdate: 2017-02-01
|
|
||||||
categories: [functions]
|
categories: [functions]
|
||||||
menu:
|
menu:
|
||||||
docs:
|
docs:
|
||||||
parent: "functions"
|
parent: functions
|
||||||
keywords: [urls]
|
keywords: [urls]
|
||||||
signature: ["relURL INPUT"]
|
signature: ["relURL INPUT"]
|
||||||
workson: []
|
|
||||||
hugoversion:
|
|
||||||
relatedfuncs: [absURL]
|
|
||||||
deprecated: false
|
|
||||||
aliases: []
|
|
||||||
---
|
---
|
||||||
|
|
||||||
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/`:
|
With multilingual configurations, use the [`relLangURL`] function instead. The URL returned by this function depends on:
|
||||||
|
|
||||||
```
|
- Whether the input begins with a slash
|
||||||
{{ "mystyle.css" | absURL }} → "https://example.com/hugo/mystyle.css"
|
- The `baseURL` in site configuration
|
||||||
{{ "mystyle.css" | relURL }} → "/hugo/mystyle.css"
|
|
||||||
{{ "http://gohugo.io/" | relURL }} → "http://gohugo.io/"
|
### Input does not begin with a slash
|
||||||
{{ "http://gohugo.io/" | absURL }} → "http://gohugo.io/"
|
|
||||||
|
If the input does not begin with a slash, the resulting URL will be correct regardless of the `baseURL`.
|
||||||
|
|
||||||
|
With `baseURL = https://example.org/`
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ relURL "" }} → /
|
||||||
|
{{ relURL "articles" }} → /articles
|
||||||
|
{{ relURL "style.css" }} → /style.css
|
||||||
```
|
```
|
||||||
|
|
||||||
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:
|
With `baseURL = https://example.org/docs/`
|
||||||
|
|
||||||
{{< code file="layouts/partials/schemaorg-metadata.html" download="schemaorg-metadata.html" >}}
|
```go-html-template
|
||||||
<script type="application/ld+json">
|
{{ relURL "" }} → /docs/
|
||||||
{
|
{{ relURL "articles" }} → /docs/articles
|
||||||
"@context" : "https://schema.org",
|
{{ relURL "style.css" }} → /docs/style.css
|
||||||
"@type" : "BlogPosting",
|
```
|
||||||
"image" : {{ apply .Params.images "absURL" "." }}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
{{< /code >}}
|
|
||||||
|
|
||||||
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.
|
### Input begins with a slash
|
||||||
|
|
||||||
{{% note "Ending Slash" %}}
|
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`.
|
||||||
`absURL` and `relURL` are smart about missing slashes, but they will *not* add a closing slash to a URL if it is not present.
|
|
||||||
|
With `baseURL = https://example.org/`
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ relURL "/" }} → /
|
||||||
|
{{ relURL "/articles" }} → /articles
|
||||||
|
{{ relURL "style.css" }} → /style.css
|
||||||
|
```
|
||||||
|
|
||||||
|
With `baseURL = https://example.org/docs/`
|
||||||
|
|
||||||
|
```go-html-template
|
||||||
|
{{ relURL "/" }} → /
|
||||||
|
{{ relURL "/articles" }} → /articles
|
||||||
|
{{ relURL "/style.css" }} → /style.css
|
||||||
|
```
|
||||||
|
|
||||||
|
{{% note %}}
|
||||||
|
The last three examples are not desirable in most situations. As a best practice, never include a leading slash when using this function.
|
||||||
{{% /note %}}
|
{{% /note %}}
|
||||||
|
|
||||||
[apply function]: /functions/apply/
|
[`relLangURL`]: /functions/rellangurl/
|
||||||
[configuration]: /getting-started/configuration/
|
|
||||||
[jsonld]: https://developers.google.com/search/docs/advanced/structured-data/intro-structured-data
|
|
||||||
[safejs]: /functions/safejs
|
|
||||||
Reference in new issue
Block a user