页面资源仅仅可由页面包可以访问, 页面包就是一个根部包含index.md, 或者 _index.md文件的目录。资源仅仅与它们所属的最低层级的页面绑定。对于不包含index.md 的目录不附属任何资源。

属性 Properties

资源类型 ResourceType :资源的媒体类型的主要类型。 比如Mime type是image/jpeg的文件具有image资源类型。 Page的资源类型值为page

在Hugo 0.80.0版本中,我们修正了一个错误,非图片类型(比如视频)返回MIME自类型,比如json.

资源名称 Name
默认值是文件名(相对于拥有页面), 可以在前言设定中设置。
Title
默认值和.Name一样也是是文件名(相对于拥有页面), 可以在前言设定中设置。
绝对URL Permalink
资源的绝对URL。 page类型的资源没有这个值。
相对URL elPermalink
资源的相对URL。 page类型的资源没有这个值。
内容 Content
资源本身的内容。对大多数资源来说,这返回一个文件内容的字符串。 这可以用来内联一些资源,比如 <script>{{ (.Resources.GetMatch "myscript.js").Content | safeJS }}</script> 或者 <img src="{{ (.Resources.GetMatch "mylogo.png").Content | base64Encode }}">
媒体类型 MediaType
资源的 MIME 类型, 如 image/jpeg.

MediaType.MainType :文件Mime type的的主类型。比如具有application/pdfMime type的文件主类型是 application

MediaType.SubType
资源文件MIME type的子类型。比如MIME Type为application/pdf的子类型是pdf. 注意,子类型和文件扩展名并不是一致–Powerpoint文件的子类型是 vnd.mspowerpoint.
MediaType.Suffixes
资源Mime 类型的可能存在的前缀 A slice of possible suffixes for the resource’s MIME type.

方法 Methods

ByType
返回指定类型的页面资源 Returns the page resources of the given type.
1
{{ .Resources.ByType "image" }}
Match
返回所有Name匹配指定的Glob模式(Glob模式例子)字符串的页面资源. 匹配是大小写无关的.
1
{{ .Resources.Match "images/*" }}
GetMatch
match 但是仅仅返回第一个匹配的页面资源

模式匹配 Pattern Matching

1
2
3
4
5
6
7
8
9
// 使用下面模式能否发现 images/sunset.jpg
// 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

页面资源的元数据通过在页面的前言设定中的名为resouces的数组或表格参数来管理。 可以使用匹配符号wildcards来批处理设置值。

name
设定Name的返回值.
title
设定 Title 的返回值
params
定制的键/值对的字典.

例子

     
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
date: "2018-01-25"
resources:
- name: header
  src: images/sunset.jpg
- params:
    icon: photo
  src: documents/photo_specs.pdf
  title: Photo Specifications
- src: documents/guide.pdf
  title: Instruction Guide
- src: documents/checklist.pdf
  title: Document Checklist
- src: documents/payment.docx
  title: Proof of Payment
- name: pdf-file-:counter
  params:
    icon: pdf
  src: '**.pdf'
- params:
    icon: word
  src: '**.docx'
title: Application
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
date = '2018-01-25'
title = 'Application'

[[resources]]
  name = 'header'
  src = 'images/sunset.jpg'

[[resources]]
  src = 'documents/photo_specs.pdf'
  title = 'Photo Specifications'

  [resources.params]
    icon = 'photo'

[[resources]]
  src = 'documents/guide.pdf'
  title = 'Instruction Guide'

[[resources]]
  src = 'documents/checklist.pdf'
  title = 'Document Checklist'

[[resources]]
  src = 'documents/payment.docx'
  title = 'Proof of Payment'

[[resources]]
  name = 'pdf-file-:counter'
  src = '**.pdf'

  [resources.params]
    icon = 'pdf'

[[resources]]
  src = '**.docx'

  [resources.params]
    icon = 'word'
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{
   "date": "2018-01-25",
   "resources": [
      {
         "name": "header",
         "src": "images/sunset.jpg"
      },
      {
         "params": {
            "icon": "photo"
         },
         "src": "documents/photo_specs.pdf",
         "title": "Photo Specifications"
      },
      {
         "src": "documents/guide.pdf",
         "title": "Instruction Guide"
      },
      {
         "src": "documents/checklist.pdf",
         "title": "Document Checklist"
      },
      {
         "src": "documents/payment.docx",
         "title": "Proof of Payment"
      },
      {
         "name": "pdf-file-:counter",
         "params": {
            "icon": "pdf"
         },
         "src": "**.pdf"
      },
      {
         "params": {
            "icon": "word"
         },
         "src": "**.docx"
      }
   ],
   "title": "Application"
}

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.

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:

     
1
2
3
4
5
resources:
- src: '*specs.pdf'
  title: 'Specification #:counter'
- name: pdf-file-:counter
  src: '**.pdf'
1
2
3
4
5
6
7
[[resources]]
  src = '*specs.pdf'
  title = 'Specification #:counter'

[[resources]]
  name = 'pdf-file-:counter'
  src = '**.pdf'
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
{
   "resources": [
      {
         "src": "*specs.pdf",
         "title": "Specification #:counter"
      },
      {
         "name": "pdf-file-:counter",
         "src": "**.pdf"
      }
   ]
}

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

Resource fileNameTitle
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"