hugo/content/en/content-management/page-resources.md
Bjørn Erik Pedersen 5e078383a7 Squashed 'docs/' changes from 785e375f..49809a03
49809a03 Merge commit '20a631b4964fc0ab9137cce1e41774cbc17de044'
20a631b4 Squashed 'themes/gohugoioTheme/' changes from b8202f539..dafc91ff1
8b58f565 Re-generate CLI docs
4653a724 Add Netlify deployment badge
2d6246bc Remove some deprecated site variables
e6777153 Improve Algolia Search Display Styling
1570999f Add missing "." in front of gitlab-ci.yaml example
b922ae7d This adds documentation to the new configDir/Environment logic from .53 (#729)
7cff379f Correctly escape multi-word taxonomy terms in example
2dfeeda4 fix typo by removing stray paren
0870bd9a Fix typo in `paginate` description
91e8be85 Fixes https://github.com/gohugoio/hugo/issues/5609
c1db65ec Make the dummy URL more obvious
b4589ff0 Fix a link
b73dcb9a Consistently use "posts" as section name in examples
7a56abbc Format definitions
a9c6fd9b Minor clarification over the last commit
5c86bdc8 Add alternative instructions for Quick Start for non-git users
dafe7ee9 Add Visual Studio Code plug-ins
110ed19e Update HUGO_VERSION
2abd031a Update page.md
b332f7b9 Update page.md
f5a8c9d4 Update static-files.md
6d0c155c Add note about relative protocol URLs
a13751ac Theme Warning: Remove note about unquoted URLs
4c8f7d68 Incorporate feedback
6f2b9cf0 Update Creating Themes Warning
40d88d98 Fix ToC example to use binary true/false
4a11f3f1 Fix typo
2dbfc0a4 Fix a typo in taxonomies
d63790ef Do not mark UndocumentedFeature issues as stale
d7aff095 Regenerate docs.json
71c0826f Update transform.Unmarshal.md

git-subtree-dir: docs
git-subtree-split: 49809a038b2691637bab7f3f2e385dde654a88b8
2019-02-01 09:01:04 +01:00

5.6 KiB

title description date categories keywords weight draft toc linktitle menu
Page Resources Page Resources -- images, other pages, documents etc. -- have page-relative URLs and their own metadata. 2018-01-24
content management
bundle
content
resources
4003 false true Page Resources
docs
parent weight
content-management 31

Properties

ResourceType
The main type of the resource. For example, a file of MIME type image/jpg has the ResourceType image.
Name
Default value is the filename (relative to the owning page). Can be set in front matter.
Title
Default value is the same as .Name. Can be set in front matter.
Permalink
The absolute URL to the resource. Resources of type page will have no value.
RelPermalink
The relative URL to the resource. Resources of type page will have no value.
Content
The content of the resource itself. For most resources, this returns a string with the contents of the file. This can be used to inline some resources, such as <script>{{ (.Resources.GetMatch "myscript.js").Content | safeJS }}</script> or <img src="{{ (.Resources.GetMatch "mylogo.png").Content | base64Encode }}">.
MediaType
The MIME type of the resource, such as image/jpg.
MediaType.MainType
The main type of the resource's MIME type. For example, a file of MIME type application/pdf has for MainType application.
MediaType.SubType
The subtype of the resource's MIME type. For example, a file of MIME type application/pdf has for SubType pdf. Note that this is not the same as the file extension - PowerPoint files have a subtype of vnd.mspowerpoint.
MediaType.Suffixes
A slice of possible suffixes for the resource's MIME type.

Methods

ByType
Returns the page resources of the given type.
{{ .Resources.ByType "image" }}
Match
Returns all the page resources (as a slice) whose Name matches the given Glob pattern (examples). The matching is case-insensitive.
{{ .Resources.Match "images/*" }}
GetMatch
Same as Match but will return the first match.

Pattern Matching

// Using Match/GetMatch to find this images/sunset.jpg ?
.Resources.Match "images/sun*" 
.Resources.Match "**/Sunset.jpg" 
.Resources.Match "images/*.jpg" 
.Resources.Match "**.jpg" 
.Resources.Match "*" 🚫
.Resources.Match "sunset.jpg" 🚫
.Resources.Match "*sunset.jpg" 🚫

Page Resources Metadata

Page Resources' metadata is managed from their page's front matter with an array/table parameter named resources. You can batch assign values using a wildcards.

{{% note %}} Resources of type page get Title etc. from their own front matter. {{% /note %}}

name
Sets the value returned in Name.

{{% warning %}} The methods Match and GetMatch use Name to match the resources. {{%/ warning %}}

title
Sets the value returned in Title
params
A map of custom key/values.

Resources metadata example

{{< code-toggle copy="false">}} title: Application date : 2018-01-25 resources :

  • src : "images/sunset.jpg" name : "header"
  • src : "documents/photo_specs.pdf" title : "Photo Specifications" params: icon : "photo"
  • src : "documents/guide.pdf" title : "Instruction Guide"
  • src : "documents/checklist.pdf" title : "Document Checklist"
  • src : "documents/payment.docx" title : "Proof of Payment"
  • src : "**.pdf" name : "pdf-file-:counter" params : icon : "pdf"
  • src : "**.docx" params : icon : "word" {{</ code-toggle >}}

From the example above:

  • sunset.jpg will receive a new Name and can now be found with .GetMatch "header".
  • documents/photo_specs.pdf will get the photo icon.
  • documents/checklist.pdf, documents/guide.pdf and documents/payment.docx will get Title as set by title.
  • Every PDF in the bundle except documents/photo_specs.pdf will get the pdf icon.
  • All PDF files will get a new Name. The name parameter contains a special placeholder :counter, so the Name will be pdf-file-1, pdf-file-2, pdf-file-3.
  • Every docx in the bundle will receive the word icon.

{{% warning %}} 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. For example, in the above example, .Params.icon is already 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. {{%/ warning %}}

The :counter placeholder in name and title

The :counter is a special placeholder recognized in name and title parameters resources.

The counter starts at 1 the first time they are used in either name or title.

For example, if a bundle has the resources photo_specs.pdf, other_specs.pdf, guide.pdf and checklist.pdf, and the front matter has specified the resources as:

{{< code-toggle copy="false">}} resources src = "*specs.pdf" title = "Specification #:counter" resources src = "**.pdf" name = "pdf-file-:counter" {{</ code-toggle >}}

the Name and Title will be assigned to the resource files as follows:

Resource file Name Title
checklist.pdf "pdf-file-1.pdf "checklist.pdf"
guide.pdf "pdf-file-2.pdf "guide.pdf"
other_specs.pdf "pdf-file-3.pdf "Specification #1"
photo_specs.pdf "pdf-file-4.pdf "Specification #2"