mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-14 20:37:55 -05:00
87de22d746
c43daf45f Update build-options.md 3ebbfedd4 Build options: Improve readability 5091bf6a0 Improve safeHTMLAttr explanation b64cbce2e Fix description of collections.Apply 6ea264b9c netlify: Hugo 0.115.4 b42e7c542 Revert "config: Remove disableLiveReload" 35ce2290e Remove excess spaces in configuration docs 2edf761de Update listed titleCaseStyle default value 887f6fb97 config: Remove disableLiveReload c9f49fb26 Fix typo 37d8569ac Remove tools associated with Atom 871d11b72 Fix URL in postprocess docs bbb17d29f Update GitLab workflow bc53ea5ce Use sentence-style capitalization for headings 7ca578786 netlify: Hugo 0.115.3 c5e010bd0 Merge branch 'tempv0.115.3' c885604bf Remove starter kits page 4c0fe269e Update mention of Netlify CMS to Decap CMS 05067175c Consistently use file name instead of filename 763dd6404 Improve multilingual config example and descriptions e5aa61ec5 Use lowercase when referring to front matter (#2132) 7ba3d0c72 docs: Refresh docs.json de8bddedf Update description of timeout configuration value e1245d9f8 netify: Hugo 0.115.2 153a36bdf Merge branch 'tempv0.115.2' 707cec754 Fix typo in figure example in shortcodes.md 128cbe1e5 Improve taxonomy template examples 4e743ec36 Improve highlight function example f96fa6805 transpile sass: Fixes typo e4a8a21f7 Compile Sass to CSS, not SCSS c1538bd00 docs: Regenerate CLI docs bd4e33436 Add titleCaseStyle none and firstupper 6ff93d478 Update quick-start.md 5c6653cb1 Update build config examples and explanation 1458d9a43 Remove the `url` parameter 6a1e92044 netlify: Hugo 0.115.1 a9d5d6f2f Merge branch 'tempv0.115.1' 4c4882384 docs: Regen docs helper d1aa1c1f5 Add link to PowerShell vs Windows PowerShell documentation 6e3b70c21 Fix link to Git installation instructions 4f8a9ca38 Clarify resources.Copy arguments ee86dd121 Update theme dc7c305cf Update theme 60c23920b Clarify caching for resources.FromString (#2120) 5bf2fef6d netlify: Hugo 0.115.0 46bde87c5 Merge branch 'tempv0.115.0' 42cc48c16 Specify target path caching for resources.ExecuteAsTemplate (#2027) a54bf4cd0 Correct the sample code of mermaid (#2119) 8c49b06fc docs: Update permalinks documentation a4818d99b Page bundles: link to info about single vs. list page templates (#2116) 3fc7744d7 snap: Document removable media access dbd08f58a Update theme df5b88633 netlify: Hugo 0.114.1 6b859834a Fix typo 9ec92cf68 Improve Dart Sass example for Netlify 2d294ece9 Add Dart Sass installation and usage documentation 4c6b77d6c Fix placement of curly braces 897812a50 Update template-debugging.md to include a jsonify example 22bca519b Update GitHub Pages hosting instructions (#2109) a964d93ce Document math functions new in v0.114.0 (#2108) 9f4cb040e netlify: Hugo 0.114.0 55b4d9221 Merge branch 'tempv0.114.0' 93c4dcf93 docs: Regen docshelper 96f03c77f docs: Regen CLI docs 8e22a228a Clarify resource media type variables (#2106) 2652da8d4 Update transform.Unmarshal.md (#2105) 92657177a Update theme 4601c1d65 Update theme a216f3145 Merge commit '3c1deaf201a35de08d23cc58f8f03682cace3349' eed8794f5 cache: Set default cache path based on $USER git-subtree-dir: docs git-subtree-split: c43daf45fdc36c254f4274a0815ea62d4d8c37e0
179 lines
7.4 KiB
Markdown
Executable file
179 lines
7.4 KiB
Markdown
Executable file
---
|
|
title: Hugo Pipes
|
|
linkTitle: Introduction
|
|
description: Hugo Pipes is Hugo's asset processing set of functions.
|
|
categories: [asset management]
|
|
keywords: []
|
|
menu:
|
|
docs:
|
|
parent: hugo-pipes
|
|
weight: 20
|
|
weight: 20
|
|
toc: true
|
|
aliases: [/assets/]
|
|
---
|
|
|
|
## Find resources in /assets
|
|
|
|
This is about the global Resources mounted inside `/assets`. For the `.Page` scoped Resources, see [Page Resources](/content-management/page-resources/).
|
|
|
|
Note that you can mount any directory into Hugo's virtual `assets` folder using the [Mount Configuration](/hugo-modules/configuration/#module-configuration-mounts).
|
|
|
|
| Function | Description |
|
|
| ------------- | ------------- |
|
|
| `resources.Get` | Get locates the file name given in Hugo's assets filesystem and creates a `Resource` object that can be used for further transformations. See [Get a resource](#get-a-resource). |
|
|
| `resources.GetRemote` | Same as `Get`, but it accepts remote URLs. See [Get a resource](#get-a-resource).|
|
|
| `resources.GetMatch` | `GetMatch` finds the first Resource matching the given pattern, or nil if none found. See Match for a more complete explanation about the rules used. |
|
|
| `resources.Match` | `Match` gets all resources matching the given base path prefix, e.g "*.png" will match all png files. The "*" does not match path delimiters (/), so if you organize your resources in sub-folders, you need to be explicit about it, e.g.: "images/*.png". To match any PNG image anywhere in the bundle you can do "\*\*.png", and to match all PNG images below the images folder, use "images/\*\*.jpg". The matching is case insensitive. Match matches by using the files name with path relative to the file system root with Unix style slashes (/) and no leading slash, e.g. "images/logo.png". See https://github.com/gobwas/glob for the full rules set.|
|
|
|
|
See the [GoDoc Page](https://pkg.go.dev/github.com/gohugoio/hugo@v0.93.1/tpl/resources) for the `resources` package for an up to date overview of all template functions in this namespace.
|
|
|
|
## Get a resource
|
|
|
|
In order to process an asset with Hugo Pipes, it must be retrieved as a `Resource` using `resources.Get` or `resources.GetRemote`.
|
|
|
|
With `resources.Get`, the first argument is a local path relative to the `assets` directory/directories:
|
|
|
|
```go-html-template
|
|
{{ $local := resources.Get "sass/main.scss" }}
|
|
```
|
|
|
|
With `resources.GetRemote`, the first argument is a remote URL:
|
|
|
|
```go-html-template
|
|
{{ $remote := resources.GetRemote "https://www.example.com/styles.scss" }}
|
|
```
|
|
|
|
`resources.Get` and `resources.GetRemote` return `nil` if the resource is not found.
|
|
|
|
{{< new-in "0.110.0" >}} You can get information about the HTTP Response using `.Data` in the returned `Resource`. This is especially useful for HEAD request without any body. The Data object contains:
|
|
|
|
StatusCode
|
|
: The HTTP status code, e.g. 200
|
|
Status
|
|
: The HTTP status text, e.g. "200 OK"
|
|
TransferEncoding
|
|
: The transfer encoding, e.g. "chunked"
|
|
ContentLength
|
|
: The content length, e.g. 1234
|
|
ContentType
|
|
: The content type, e.g. "text/html"
|
|
|
|
### Caching
|
|
|
|
By default, Hugo calculates a cache key based on the `URL` and the `options` (e.g. headers) given.
|
|
|
|
{{< new-in "0.97.0" >}} You can override this by setting a `key` in the options map. This can be used to get more fine grained control over how often a remote resource is fetched, e.g.:
|
|
|
|
|
|
```go-html-template
|
|
{{ $cacheKey := print $url (now.Format "2006-01-02") }}
|
|
{{ $resource := resource.GetRemote $url (dict "key" $cacheKey) }}
|
|
```
|
|
|
|
### Error handling
|
|
|
|
The return value from `resources.GetRemote` includes an `.Err` method that will return an error if the call failed. If you want to just log any error as a `WARNING` you can use a construct similar to the one below.
|
|
|
|
```go-html-template
|
|
{{ with resources.GetRemote "https://gohugo.io/images/gohugoio-card-1.png" }}
|
|
{{ with .Err }}
|
|
{{ warnf "%s" . }}
|
|
{{ else }}
|
|
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
|
|
{{ end }}
|
|
{{ end }}
|
|
```
|
|
|
|
Note that if you do not handle `.Err` yourself, Hugo will fail the build the first time you start using the `Resource` object.
|
|
|
|
### Remote options
|
|
|
|
When fetching a remote `Resource`, `resources.GetRemote` takes an optional options map as the second argument, e.g.:
|
|
|
|
```go-html-template
|
|
{{ $resource := resources.GetRemote "https://example.org/api" (dict "headers" (dict "Authorization" "Bearer abcd")) }}
|
|
```
|
|
|
|
If you need multiple values for the same header key, use a slice:
|
|
|
|
```go-html-template
|
|
{{ $resource := resources.GetRemote "https://example.org/api" (dict "headers" (dict "X-List" (slice "a" "b" "c"))) }}
|
|
```
|
|
|
|
You can also change the request method and set the request body:
|
|
|
|
```go-html-template
|
|
{{ $postResponse := resources.GetRemote "https://example.org/api" (dict
|
|
"method" "post"
|
|
"body" `{"complete": true}`
|
|
"headers" (dict
|
|
"Content-Type" "application/json"
|
|
)
|
|
)}}
|
|
```
|
|
|
|
### Caching of remote resources
|
|
|
|
Remote resources fetched with `resources.GetRemote` will be cached on disk. See [Configure File Caches](/getting-started/configuration/#configure-file-caches) for details.
|
|
|
|
## Copy a resource
|
|
|
|
{{< new-in "0.100.0" >}}
|
|
|
|
Use `resources.Copy` to copy a page resource or a global resource. Commonly used to change a resource's published path, `resources.Copy` takes two arguments: the target path relative to the root of the `publishDir` (with or without a leading `/`), and the resource to copy.
|
|
|
|
```go-html-template
|
|
{{ with resources.Get "img/a.jpg" }}
|
|
{{ with .Resize "300x" }}
|
|
{{ with resources.Copy "img/a-new.jpg" . }}
|
|
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
|
|
{{ end }}
|
|
{{ end }}
|
|
{{ end }}
|
|
```
|
|
|
|
{{% note %}}
|
|
The target path must be different than the source path, as shown in the example above. See GitHub issue [#10412](https://github.com/gohugoio/hugo/issues/10412).
|
|
{{% /note %}}
|
|
|
|
## Asset directory
|
|
|
|
Asset files must be stored in the asset directory. This is `/assets` by default, but can be configured via the configuration file's `assetDir` key.
|
|
|
|
### Asset publishing
|
|
|
|
Hugo publishes assets to the `publishDir` (typically `public`) when you invoke `.Permalink`, `.RelPermalink`, or `.Publish`. You can use `.Content` to inline the asset.
|
|
|
|
## Go Pipes
|
|
|
|
For improved readability, the Hugo Pipes examples of this documentation will be written using [Go Pipes](/templates/introduction/#pipes):
|
|
|
|
```go-html-template
|
|
{{ $style := resources.Get "sass/main.scss" | resources.ToCSS | resources.Minify | resources.Fingerprint }}
|
|
<link rel="stylesheet" href="{{ $style.Permalink }}">
|
|
```
|
|
|
|
## Method aliases
|
|
|
|
Each Hugo Pipes `resources` transformation method uses a __camelCased__ alias (`toCSS` for `resources.ToCSS`).
|
|
Non-transformation methods deprived of such aliases are `resources.Get`, `resources.FromString`, `resources.ExecuteAsTemplate` and `resources.Concat`.
|
|
|
|
The example above can therefore also be written as follows:
|
|
|
|
```go-html-template
|
|
{{ $style := resources.Get "sass/main.scss" | toCSS | minify | fingerprint }}
|
|
<link rel="stylesheet" href="{{ $style.Permalink }}">
|
|
```
|
|
|
|
## Caching
|
|
|
|
Hugo Pipes invocations are cached based on the entire *pipe chain*.
|
|
|
|
An example of a pipe chain is:
|
|
|
|
```go-html-template
|
|
{{ $mainJs := resources.Get "js/main.js" | js.Build "main.js" | minify | fingerprint }}
|
|
```
|
|
|
|
The pipe chain is only invoked the first time it is encountered in a site build, and results are otherwise loaded from cache. As such, Hugo Pipes can be used in templates which are executed thousands or millions of times without negatively impacting the build performance.
|