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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
|
---
title: Collections
permalink: /docs/collections/
---
Not everything is a post or a page. Maybe you want to document the various
methods in your open source project, members of a team, or talks at a
conference. Collections allow you to define a new type of document that behave
like Pages or Posts do normally, but also have their own unique properties and
namespace.
## Using Collections
To start using collections, follow these 3 steps:
* [Step 1: Tell Jekyll to read in your collection](#step1)
* [Step 2: Add your content](#step2)
* [Step 3: Optionally render your collection's documents into independent files](#step3)
### Step 1: Tell Jekyll to read in your collection {#step1}
Add the following to your site's `_config.yml` file, replacing `my_collection`
with the name of your collection:
```yaml
collections:
- my_collection
```
You can optionally specify metadata for your collection in the configuration:
```yaml
collections:
my_collection:
foo: bar
```
Default attributes can also be set for a collection:
```yaml
defaults:
- scope:
path: ""
type: my_collection
values:
layout: page
```
<div class="note">
<h5>Gather your collections {%- include docs_version_badge.html version="3.7.0" -%}</h5>
<p>You can optionally specify a directory to store all your collections in the same place with <code>collections_dir: my_collections</code>.</p>
<p>Then Jekyll will look in <code>my_collections/_books</code> for the <code>books</code> collection, and
in <code>my_collections/_recipes</code> for the <code>recipes</code> collection.</p>
</div>
<div class="note warning">
<h5>Be sure to move posts into custom collections directory</h5>
<p>If you specify a directory to store all your collections in the same place with <code>collections_dir: my_collections</code>, then you will need to move your <code>_posts</code> directory to <code>my_collections/_posts</code>. Note that, the name of your collections directory cannot start with an underscore (`_`).</p>
</div>
### Step 2: Add your content {#step2}
Create a corresponding folder (e.g. `<source>/_my_collection`) and add
documents. YAML front matter is processed if the front matter exists, and everything
after the front matter is pushed into the document's `content` attribute. If no YAML front
matter is provided, Jekyll will not generate the file in your collection.
<div class="note info">
<h5>Be sure to name your directories correctly</h5>
<p>
The folder must be named identically to the collection you defined in
your <code>_config.yml</code> file, with the addition of the preceding <code>_</code> character.
</p>
</div>
### Step 3: Optionally render your collection's documents into independent files {#step3}
If you'd like Jekyll to create a public-facing, rendered version of each
document in your collection, set the `output` key to `true` in your collection
metadata in your `_config.yml`:
```yaml
collections:
my_collection:
output: true
```
This will produce a file for each document in the collection.
For example, if you have `_my_collection/some_subdir/some_doc.md`,
it will be rendered using Liquid and the Markdown converter of your
choice and written out to `<dest>/my_collection/some_subdir/some_doc.html`.
If you wish a specific page to be shown when accessing `/my_collection/`,
simply add `permalink: /my_collection/index.html` to a page.
To list items from the collection, on that page or any other, you can use:
{% raw %}
```liquid
{% for item in site.my_collection %}
<h2>{{ item.title }}</h2>
<p>{{ item.description }}</p>
<p><a href="{{ item.url }}">{{ item.title }}</a></p>
{% endfor %}
```
{% endraw %}
<div class="note info">
<h5>Don't forget to add YAML for processing</h5>
<p>
Files in collections that do not have front matter are treated as
<a href="/docs/static-files">static files</a> and simply copied to their
output location without processing.
</p>
</div>
## Configuring permalinks for collections {#permalinks}
If you wish to specify a custom pattern for the URLs where your Collection pages
will reside, you may do so with the [`permalink` property](../permalinks/):
```yaml
collections:
my_collection:
output: true
permalink: /:collection/:name
```
### Examples
For a collection with the following source file structure,
```
_my_collection/
└── some_subdir
└── some_doc.md
```
each of the following `permalink` configurations will produce the document structure shown below it.
* **Default**
Same as `permalink: /:collection/:path`.
```
_site/
├── my_collection
│ └── some_subdir
│ └── some_doc.html
...
```
* `permalink: pretty`
Same as `permalink: /:collection/:path/`.
```
_site/
├── my_collection
│ └── some_subdir
│ └── some_doc
│ └── index.html
...
```
* `permalink: /doc/:path`
```
_site/
├── doc
│ └── some_subdir
│ └── some_doc.html
...
```
* `permalink: /doc/:name`
```
_site/
├── doc
│ └── some_doc.html
...
```
* `permalink: /:name`
```
_site/
├── some_doc.html
...
```
### Template Variables
<div class="mobile-side-scroller">
<table>
<thead>
<tr>
<th>Variable</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p><code>:collection</code></p>
</td>
<td>
<p>Label of the containing collection.</p>
</td>
</tr>
<tr>
<td>
<p><code>:path</code></p>
</td>
<td>
<p>Path to the document relative to the collection's directory.</p>
</td>
</tr>
<tr>
<td>
<p><code>:name</code></p>
</td>
<td>
<p>The document's base filename, with every sequence of spaces
and non-alphanumeric characters replaced by a hyphen.</p>
</td>
</tr>
<tr>
<td>
<p><code>:title</code></p>
</td>
<td>
<p>
The <code>:title</code> template variable will take the
<code>slug</code> <a href="/docs/frontmatter/">front matter</a>
variable value if any is present in the document; if none is
defined then <code>:title</code> will be equivalent to
<code>:name</code>, aka the slug generated from the filename.
</p>
</td>
</tr>
<tr>
<td>
<p><code>:output_ext</code></p>
</td>
<td>
<p>Extension of the output file. (Included by default and usually unnecessary.)</p>
</td>
</tr>
</tbody>
</table>
</div>
## Liquid Attributes
### Collections
Each collection is accessible as a field on the `site` variable. For example, if
you want to access the `albums` collection found in `_albums`, you'd use
`site.albums`.
Each collection is itself an array of documents (e.g., `site.albums` is an array of documents, much like `site.pages` and
`site.posts`). See the table below for how to access attributes of those documents.
The collections are also available under `site.collections`, with the metadata
you specified in your `_config.yml` (if present) and the following information:
<div class="mobile-side-scroller">
<table>
<thead>
<tr>
<th>Variable</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p><code>label</code></p>
</td>
<td>
<p>
The name of your collection, e.g. <code>my_collection</code>.
</p>
</td>
</tr>
<tr>
<td>
<p><code>docs</code></p>
</td>
<td>
<p>
An array of <a href="#documents">documents</a>.
</p>
</td>
</tr>
<tr>
<td>
<p><code>files</code></p>
</td>
<td>
<p>
An array of static files in the collection.
</p>
</td>
</tr>
<tr>
<td>
<p><code>relative_directory</code></p>
</td>
<td>
<p>
The path to the collection's source directory, relative to the site
source.
</p>
</td>
</tr>
<tr>
<td>
<p><code>directory</code></p>
</td>
<td>
<p>
The full path to the collections's source directory.
</p>
</td>
</tr>
<tr>
<td>
<p><code>output</code></p>
</td>
<td>
<p>
Whether the collection's documents will be output as individual
files.
</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="note info">
<h5>A Hard-Coded Collection</h5>
<p>In addition to any collections you create yourself, the
<code>posts</code> collection is hard-coded into Jekyll. It exists whether
you have a <code>_posts</code> directory or not. This is something to note
when iterating through <code>site.collections</code> as you may need to
filter it out.</p>
<p>You may wish to use filters to find your collection:
<code>{% raw %}{{ site.collections | where: "label", "myCollection" | first }}{% endraw %}</code></p>
</div>
<div class="note info">
<h5>Collections and Time</h5>
<p>Except for documents in hard-coded default collection <code>posts</code>, all documents in collections
you create, are accessible via Liquid irrespective of their assigned date, if any, and therefore renderable.
</p>
<p>However documents are attempted to be written to disk only if the concerned collection
metadata has <code>output: true</code>. Additionally, future-dated documents are only written if
<code>site.future</code> <em>is also true</em>.
</p>
<p>More fine-grained control over documents being written to disk can be exercised by setting
<code>published: false</code> (<em><code>true</code> by default</em>) in the document's front matter.
</p>
</div>
### Documents
In addition to any YAML Front Matter provided in the document's corresponding
file, each document has the following attributes:
<div class="mobile-side-scroller">
<table>
<thead>
<tr>
<th>Variable</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p><code>content</code></p>
</td>
<td>
<p>
The (unrendered) content of the document. If no YAML Front Matter is
provided, Jekyll will not generate the file in your collection. If
YAML Front Matter is used, then this is all the contents of the file
after the terminating
`---` of the front matter.
</p>
</td>
</tr>
<tr>
<td>
<p><code>output</code></p>
</td>
<td>
<p>
The rendered output of the document, based on the
<code>content</code>.
</p>
</td>
</tr>
<tr>
<td>
<p><code>path</code></p>
</td>
<td>
<p>
The full path to the document's source file.
</p>
</td>
</tr>
<tr>
<td>
<p><code>relative_path</code></p>
</td>
<td>
<p>
The path to the document's source file relative to the site source.
</p>
</td>
</tr>
<tr>
<td>
<p><code>url</code></p>
</td>
<td>
<p>
The URL of the rendered collection. The file is only written to the destination when the collection to which it belongs has <code>output: true</code> in the site's configuration.
</p>
</td>
</tr>
<tr>
<td>
<p><code>collection</code></p>
</td>
<td>
<p>
The name of the document's collection.
</p>
</td>
</tr>
<tr>
<td>
<p><code>date</code></p>
</td>
<td>
<p>
The date of the document's collection.
</p>
</td>
</tr>
</tbody>
</table>
</div>
## Accessing Collection Attributes
Attributes from the YAML front matter can be accessed as data anywhere in the
site. Using the above example for configuring a collection as `site.albums`,
you might have front matter in an individual file structured as follows (which
must use a supported markup format, and cannot be saved with a `.yaml`
extension):
```yaml
title: "Josquin: Missa De beata virgine and Missa Ave maris stella"
artist: "The Tallis Scholars"
director: "Peter Phillips"
works:
- title: "Missa De beata virgine"
composer: "Josquin des Prez"
tracks:
- title: "Kyrie"
duration: "4:25"
- title: "Gloria"
duration: "9:53"
- title: "Credo"
duration: "9:09"
- title: "Sanctus & Benedictus"
duration: "7:47"
- title: "Agnus Dei I, II & III"
duration: "6:49"
```
Every album in the collection could be listed on a single page with a template:
```liquid
{% raw %}
{% for album in site.albums %}
<h2>{{ album.title }}</h2>
<p>Performed by {{ album.artist }}{% if album.director %}, directed by {{ album.director }}{% endif %}</p>
{% for work in album.works %}
<h3>{{ work.title }}</h3>
<p>Composed by {{ work.composer }}</p>
<ul>
{% for track in work.tracks %}
<li>{{ track.title }} ({{ track.duration }})</li>
{% endfor %}
</ul>
{% endfor %}
{% endfor %}
{% endraw %}
```
|