Update page resources documentation

This commit is contained in:
Joe Mooring 2024-08-25 12:30:08 -07:00 committed by GitHub
parent ca802fbec1
commit 7c766e724e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,6 @@
--- ---
title: Page resources title: Page resources
description: Page resources -- images, other pages, documents, etc. -- have page-relative URLs and their own metadata. description: Use page resources to logically associate assets with a page.
categories: [content management] categories: [content management]
keywords: [bundle,content,resources] keywords: [bundle,content,resources]
menu: menu:
@ -37,82 +37,83 @@ content
└── index.md (root of page bundle) └── index.md (root of page bundle)
``` ```
## Properties ## Examples
ResourceType Use any of these methods on a `Page` object to capture page resources:
: The main type of the resource's [Media Type](/templates/output-formats/#media-types). For example, a file of MIME type `image/jpeg` has the ResourceType `image`. A `Page` will have `ResourceType` with value `page`.
Name - [`Resources.ByType`]
: Default value is the file name (relative to the owning page). Can be set in front matter. - [`Resources.Get`]
- [`Resources.GetMatch`]
- [`Resources.Match`]
Title Once you have captured a resource, use any of the applicable [`Resource`] methods to return a value or perform an action.
: Default value is the same as `.Name`. Can be set in front matter.
Permalink [`Resource`]: /methods/resource
: The absolute URL to the resource. Resources of type `page` will have no value. [`Resources.ByType`]: /methods/page/resources#bytype
[`Resources.GetMatch`]: /methods/page/resources#getmatch
[`Resources.Get`]: /methods/page/resources#get
[`Resources.Match`]: /methods/page/resources#match
RelPermalink The following examples assume this content structure:
: The relative URL to the resource. Resources of type `page` will have no value.
Content ```text
: The content of the resource itself. For most resources, this returns a string content/
with the contents of the file. Use this to create inline resources. └── example/
├── data/
│ └── books.json <-- page resource
├── images/
│ ├── a.jpg <-- page resource
│ └── b.jpg <-- page resource
├── snippets/
│ └── text.md <-- page resource
└── index.md
```
Render a single image, and throw an error if the file does not exist:
```go-html-template ```go-html-template
{{ with .Resources.GetMatch "script.js" }} {{ $path := "images/a.jpg" }}
<script>{{ .Content | safeJS }}</script> {{ with .Resources.Get $path }}
{{ end }} <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ else }}
{{ with .Resources.GetMatch "style.css" }} {{ errorf "Unable to get page resource %q" $path }}
<style>{{ .Content | safeCSS }}</style>
{{ end }}
{{ with .Resources.GetMatch "img.png" }}
<img src="data:{{ .MediaType.Type }};base64,{{ .Content | base64Encode }}">
{{ end }} {{ end }}
``` ```
MediaType.Type Render all images, resized to 300 px wide:
: The media type (formerly known as a MIME type) of the resource (e.g., `image/jpeg`).
MediaType.MainType
: The main type of the resource's media type (e.g., `image`).
MediaType.SubType
: The subtype of the resource's type (e.g., `jpeg`). This may or may not correspond to the file suffix.
MediaType.Suffixes
: A slice of possible file suffixes for the resource's media type (e.g., `[jpg jpeg jpe jif jfif]`).
## Methods
ByType
: Returns the page resources of the given type.
```go-html-template ```go-html-template
{{ .Resources.ByType "image" }} {{ range .Resources.ByType "image" }}
{{ with .Resize "300x" }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
{{ end }}
``` ```
Match
: Returns all the page resources (as a slice) whose `Name` matches the given Glob pattern ([examples](https://github.com/gobwas/glob/blob/master/readme.md)). The matching is case-insensitive. Render the markdown snippet:
```go-html-template ```go-html-template
{{ .Resources.Match "images/*" }} {{ with .Resources.Get "snippets/text.md" }}
{{ .Content }}
{{ end }}
``` ```
GetMatch List the titles in the data file, and throw an error if the file does not exist.
: Same as `Match` but will return the first match.
### Pattern matching ```go-html-template
{{ $path := "data/books.json" }}
```go {{ with .Resources.Get $path }}
// Using Match/GetMatch to find this images/sunset.jpg ? {{ with . | transform.Unmarshal }}
.Resources.Match "images/sun*" ✅ <p>Books:</p>
.Resources.Match "**/sunset.jpg" ✅ <ul>
.Resources.Match "images/*.jpg" ✅ {{ range . }}
.Resources.Match "**.jpg" ✅ <li>{{ .title }}</li>
.Resources.Match "*" 🚫 {{ end }}
.Resources.Match "sunset.jpg" 🚫 </ul>
.Resources.Match "*sunset.jpg" 🚫 {{ end }}
{{ else }}
{{ errorf "Unable to get page resource %q" $path }}
{{ end }}
``` ```
## Metadata ## Metadata
@ -124,21 +125,21 @@ Resources of type `page` get `Title` etc. from their own front matter.
{{% /note %}} {{% /note %}}
name name
: Sets the value returned in `Name`. : (`string`) Sets the value returned in `Name`.
{{% note %}} {{% note %}}
The methods `Match`, `Get` and `GetMatch` use `Name` to match the resources. The methods `Match`, `Get` and `GetMatch` use `Name` to match the resources.
{{% /note %}} {{% /note %}}
title title
: Sets the value returned in `Title` : (`string`) Sets the value returned in `Title`
params params
: A map of custom key-value pairs. : (`map`) A map of custom key-value pairs.
### Resources metadata example ### Resources metadata example
{{< code-toggle >}} {{< code-toggle file=content/example.md fm=true >}}
title: Application title: Application
date : 2018-01-25 date : 2018-01-25
resources : resources :
@ -173,7 +174,7 @@ From the example above:
- Every docx in the bundle will receive the `word` icon. - Every docx in the bundle will receive the `word` icon.
{{% note %}} {{% note %}}
The __order matters__ --- Only the **first set** values of the `title`, `name` and `params`-**keys** will be used. Consecutive parameters will be set only for the ones not already set. In the above example, `.Params.icon` is first set to `"photo"` in `src = "documents/photo_specs.pdf"`. So that would not get overridden to `"pdf"` by the later set `src = "**.pdf"` rule. The order matters; only the first set values of the `title`, `name` and `params` keys will be used. Consecutive parameters will be set only for the ones not already set. In the above example, `.Params.icon` is first set to `"photo"` in `src = "documents/photo_specs.pdf"`. So that would not get overridden to `"pdf"` by the later set `src = "**.pdf"` rule.
{{% /note %}} {{% /note %}}
### The `:counter` placeholder in `name` and `title` ### The `:counter` placeholder in `name` and `title`