lists.md (19807B)
1 --- 2 title: Lists of Content in Hugo 3 linktitle: List Templates 4 description: Lists have a specific meaning and usage in Hugo when it comes to rendering your site homepage, section page, taxonomy list, or taxonomy terms list. 5 date: 2017-02-01 6 publishdate: 2017-02-01 7 lastmod: 2017-02-01 8 categories: [templates] 9 keywords: [lists,sections,rss,taxonomies,terms] 10 menu: 11 docs: 12 parent: "templates" 13 weight: 22 14 weight: 22 15 sections_weight: 22 16 draft: false 17 aliases: [/templates/list/,/layout/indexes/] 18 toc: true 19 --- 20 21 ## What is a List Page Template? 22 23 {{< youtube 8b2YTSMdMps >}} 24 25 A list page template is a template used to render multiple pieces of content in a single HTML page. The exception to this rule is the homepage, which is still a list but has its own [dedicated template][homepage]. 26 27 Hugo uses the term *list* in its truest sense; i.e. a sequential arrangement of material, especially in alphabetical or numerical order. Hugo uses list templates on any output HTML page where content is traditionally listed: 28 29 * [Taxonomy terms pages][taxterms] 30 * [Taxonomy list pages][taxlists] 31 * [Section list pages][sectiontemps] 32 * [RSS][rss] 33 34 For template lookup order, see [Template Lookup](/templates/lookup-order/). 35 36 The idea of a list page comes from the [hierarchical mental model of the web][mentalmodel] and is best demonstrated visually: 37 38 [![Image demonstrating a hierarchical website sitemap.](/images/site-hierarchy.svg)](/images/site-hierarchy.svg) 39 40 ## List Defaults 41 42 ### Default Templates 43 44 Since section lists and taxonomy lists (N.B., *not* [taxonomy terms lists][taxterms]) are both *lists* with regards to their templates, both have the same terminating default of `_default/list.html` or `themes/<THEME>/layouts/_default/list.html` in their lookup order. In addition, both [section lists][sectiontemps] and [taxonomy lists][taxlists] have their own default list templates in `_default`. 45 46 See [Template Lookup Order](/templates/lookup-order/) for the complete reference. 47 48 ## Add Content and Front Matter to List Pages 49 50 Since v0.18, [everything in Hugo is a `Page`][bepsays]. This means list pages and the homepage can have associated content files (i.e. `_index.md`) that contain page metadata (i.e., front matter) and content. 51 52 This new model allows you to include list-specific front matter via `.Params` and also means that list templates (e.g., `layouts/_default/list.html`) have access to all [page variables][pagevars]. 53 54 {{% note %}} 55 It is important to note that all `_index.md` content files will render according to a *list* template and not according to a [single page template](/templates/single-page-templates/). 56 {{% /note %}} 57 58 ### Example Project Directory 59 60 The following is an example of a typical Hugo project directory's content: 61 62 ``` 63 . 64 ... 65 ├── content 66 | ├── posts 67 | | ├── _index.md 68 | | ├── post-01.md 69 | | └── post-02.md 70 | └── quote 71 | | ├── quote-01.md 72 | | └── quote-02.md 73 ... 74 ``` 75 76 Using the above example, let's assume you have the following in `content/posts/_index.md`: 77 78 {{< code file="content/posts/_index.md" >}} 79 --- 80 title: My Go Journey 81 date: 2017-03-23 82 publishdate: 2017-03-24 83 --- 84 85 I decided to start learning Go in March 2017. 86 87 Follow my journey through this new blog. 88 {{< /code >}} 89 90 You can now access this `_index.md`'s' content in your list template: 91 92 {{< code file="layouts/_default/list.html" download="list.html" >}} 93 {{ define "main" }} 94 <main> 95 <article> 96 <header> 97 <h1>{{.Title}}</h1> 98 </header> 99 <!-- "{{.Content}}" pulls from the markdown content of the corresponding _index.md --> 100 {{.Content}} 101 </article> 102 <ul> 103 <!-- Ranges through content/posts/*.md --> 104 {{ range .Pages }} 105 <li> 106 <a href="{{.Permalink}}">{{.Date.Format "2006-01-02"}} | {{.Title}}</a> 107 </li> 108 {{ end }} 109 </ul> 110 </main> 111 {{ end }} 112 {{< /code >}} 113 114 This above will output the following HTML: 115 116 {{< code file="example.com/posts/index.html" copy="false" >}} 117 <!--top of your baseof code--> 118 <main> 119 <article> 120 <header> 121 <h1>My Go Journey</h1> 122 </header> 123 <p>I decided to start learning Go in March 2017.</p> 124 <p>Follow my journey through this new blog.</p> 125 </article> 126 <ul> 127 <li><a href="/posts/post-01/">Post 1</a></li> 128 <li><a href="/posts/post-02/">Post 2</a></li> 129 </ul> 130 </main> 131 <!--bottom of your baseof--> 132 {{< /code >}} 133 134 ### List Pages Without `_index.md` 135 136 You do *not* have to create an `_index.md` file for every list page (i.e. section, taxonomy, taxonomy terms, etc) or the homepage. If Hugo does not find an `_index.md` within the respective content section when rendering a list template, the page will be created but with no `{{.Content}}` and only the default values for `.Title` etc. 137 138 Using this same `layouts/_default/list.html` template and applying it to the `quotes` section above will render the following output. Note that `quotes` does not have an `_index.md` file to pull from: 139 140 {{< code file="example.com/quote/index.html" copy="false" >}} 141 <!--baseof--> 142 <main> 143 <article> 144 <header> 145 <!-- Hugo assumes that .Title is the name of the section since there is no _index.md content file from which to pull a "title:" field --> 146 <h1>Quotes</h1> 147 </header> 148 </article> 149 <ul> 150 <li><a href="https://example.com/quote/quotes-01/">Quote 1</a></li> 151 <li><a href="https://example.com/quote/quotes-02/">Quote 2</a></li> 152 </ul> 153 </main> 154 <!--baseof--> 155 {{< /code >}} 156 157 {{% note %}} 158 The default behavior of Hugo is to pluralize list titles; hence the inflection of the `quote` section to "Quotes" when called with the `.Title` [page variable](/variables/page/). You can change this via the `pluralizeListTitles` directive in your [site configuration](/getting-started/configuration/). 159 {{% /note %}} 160 161 ## Example List Templates 162 163 ### Section Template 164 165 This list template has been modified slightly from a template originally used in [spf13.com](https://spf13.com/). It makes use of [partial templates][partials] for the chrome of the rendered page rather than using a [base template][base]. The examples that follow also use the [content view templates][views] `li.html` or `summary.html`. 166 167 {{< code file="layouts/section/posts.html" >}} 168 {{ partial "header.html" . }} 169 {{ partial "subheader.html" . }} 170 <main> 171 <div> 172 <h1>{{ .Title }}</h1> 173 <ul> 174 <!-- Renders the li.html content view for each content/posts/*.md --> 175 {{ range .Pages }} 176 {{ .Render "li"}} 177 {{ end }} 178 </ul> 179 </div> 180 </main> 181 {{ partial "footer.html" . }} 182 {{< /code >}} 183 184 ### Taxonomy Template 185 186 {{< code file="layouts/_default/taxonomy.html" download="taxonomy.html" >}} 187 {{ define "main" }} 188 <main> 189 <div> 190 <h1>{{ .Title }}</h1> 191 <!-- ranges through each of the content files associated with a particular taxonomy term and renders the summary.html content view --> 192 {{ range .Pages }} 193 {{ .Render "summary"}} 194 {{ end }} 195 </div> 196 </main> 197 {{ end }} 198 {{< /code >}} 199 200 ## Order Content 201 202 Hugo lists render the content based on metadata you provide in [front matter][]. In addition to sane defaults, Hugo also ships with multiple methods to make quick work of ordering content inside list templates: 203 204 ### Default: Weight > Date > LinkTitle > FilePath 205 206 {{< code file="layouts/partials/default-order.html" >}} 207 <ul> 208 {{ range .Pages }} 209 <li> 210 <h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1> 211 <time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time> 212 </li> 213 {{ end }} 214 </ul> 215 {{< /code >}} 216 217 ### By Weight 218 219 Lower weight gets higher precedence. So content with lower weight will come first. 220 221 {{< code file="layouts/partials/by-weight.html" >}} 222 <ul> 223 {{ range .Pages.ByWeight }} 224 <li> 225 <h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1> 226 <time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time> 227 </li> 228 {{ end }} 229 </ul> 230 {{< /code >}} 231 232 ### By Date 233 234 {{< code file="layouts/partials/by-date.html" >}} 235 <ul> 236 <!-- orders content according to the "date" field in front matter --> 237 {{ range .Pages.ByDate }} 238 <li> 239 <h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1> 240 <time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time> 241 </li> 242 {{ end }} 243 </ul> 244 {{< /code >}} 245 246 ### By Publish Date 247 248 {{< code file="layouts/partials/by-publish-date.html" >}} 249 <ul> 250 <!-- orders content according to the "publishdate" field in front matter --> 251 {{ range .Pages.ByPublishDate }} 252 <li> 253 <h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1> 254 <time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time> 255 </li> 256 {{ end }} 257 </ul> 258 {{< /code >}} 259 260 ### By Expiration Date 261 262 {{< code file="layouts/partials/by-expiry-date.html" >}} 263 <ul> 264 {{ range .Pages.ByExpiryDate }} 265 <li> 266 <h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1> 267 <time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time> 268 </li> 269 {{ end }} 270 </ul> 271 {{< /code >}} 272 273 ### By Last Modified Date 274 275 {{< code file="layouts/partials/by-last-mod.html" >}} 276 <ul> 277 <!-- orders content according to the "lastmod" field in front matter --> 278 {{ range .Pages.ByLastmod }} 279 <li> 280 <h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1> 281 <time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time> 282 </li> 283 {{ end }} 284 </ul> 285 {{< /code >}} 286 287 ### By Length 288 289 {{< code file="layouts/partials/by-length.html" >}} 290 <ul> 291 <!-- orders content according to content length in ascending order (i.e., the shortest content will be listed first) --> 292 {{ range .Pages.ByLength }} 293 <li> 294 <h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1> 295 <time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time> 296 </li> 297 {{ end }} 298 </ul> 299 {{< /code >}} 300 301 ### By Title 302 303 {{< code file="layouts/partials/by-title.html" >}} 304 <ul> 305 <!-- ranges through content in ascending order according to the "title" field set in front matter --> 306 {{ range .Pages.ByTitle }} 307 <li> 308 <h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1> 309 <time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time> 310 </li> 311 {{ end }} 312 </ul> 313 {{< /code >}} 314 315 ### By Link Title 316 317 {{< code file="layouts/partials/by-link-title.html" >}} 318 <ul> 319 <!-- ranges through content in ascending order according to the "linktitle" field in front matter. If a "linktitle" field is not set, the range will start with content that only has a "title" field and use that value for .LinkTitle --> 320 {{ range .Pages.ByLinkTitle }} 321 <li> 322 <h1><a href="{{ .Permalink }}">{{ .LinkTitle }}</a></h1> 323 <time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time> 324 </li> 325 {{ end }} 326 </ul> 327 {{< /code >}} 328 329 ### By Parameter 330 331 Order based on the specified front matter parameter. Content that does not have the specified front matter field will use the site's `.Site.Params` default. If the parameter is not found at all in some entries, those entries will appear together at the end of the ordering. 332 333 {{< code file="layouts/partials/by-rating.html" >}} 334 <!-- Ranges through content according to the "rating" field set in front matter --> 335 {{ range (.Pages.ByParam "rating") }} 336 <!-- ... --> 337 {{ end }} 338 {{< /code >}} 339 340 If the targeted front matter field is nested beneath another field, you can access the field using dot notation. 341 342 {{< code file="layouts/partials/by-nested-param.html" >}} 343 {{ range (.Pages.ByParam "author.last_name") }} 344 <!-- ... --> 345 {{ end }} 346 {{< /code >}} 347 348 ### Reverse Order 349 350 Reversing order can be applied to any of the above methods. The following uses `ByDate` as an example: 351 352 {{< code file="layouts/partials/by-date-reverse.html" >}} 353 <ul> 354 {{ range .Pages.ByDate.Reverse }} 355 <li> 356 <h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1> 357 <time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time> 358 </li> 359 {{ end }} 360 </ul> 361 {{< /code >}} 362 363 ## Group Content 364 365 Hugo provides some functions for grouping pages by Section, Type, Date, etc. 366 367 ### By Page Field 368 369 {{< code file="layouts/partials/by-page-field.html" >}} 370 <!-- Groups content according to content section. The ".Key" in this instance will be the section's title. --> 371 {{ range .Pages.GroupBy "Section" }} 372 <h3>{{ .Key }}</h3> 373 <ul> 374 {{ range .Pages }} 375 <li> 376 <a href="{{ .Permalink }}">{{ .Title }}</a> 377 <div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div> 378 </li> 379 {{ end }} 380 </ul> 381 {{ end }} 382 {{< /code >}} 383 384 In the above example, you may want `{{.Title}}` to point the `title` field you have added to your `_index.md` file instead. You can access this value using the [`.GetPage` function][getpage]: 385 386 {{< code file="layouts/partials/by-page-field.html" >}} 387 <!-- Groups content according to content section.--> 388 {{ range .Pages.GroupBy "Section" }} 389 <!-- Checks for existence of _index.md for a section; if available, pulls from "title" in front matter --> 390 {{ with $.Site.GetPage "section" .Key }} 391 <h3>{{.Title}}</h3> 392 {{ else }} 393 <!-- If no _index.md is available, ".Key" defaults to the section title and filters to title casing --> 394 <h3>{{ .Key | title }}</h3> 395 {{ end }} 396 <ul> 397 {{ range .Pages }} 398 <li> 399 <a href="{{ .Permalink }}">{{ .Title }}</a> 400 <div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div> 401 </li> 402 {{ end }} 403 </ul> 404 {{ end }} 405 {{< /code >}} 406 407 ### By Date 408 409 {{< code file="layouts/partials/by-page-date.html" >}} 410 <!-- Groups content by month according to the "date" field in front matter --> 411 {{ range .Pages.GroupByDate "2006-01" }} 412 <h3>{{ .Key }}</h3> 413 <ul> 414 {{ range .Pages }} 415 <li> 416 <a href="{{ .Permalink }}">{{ .Title }}</a> 417 <div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div> 418 </li> 419 {{ end }} 420 </ul> 421 {{ end }} 422 {{< /code >}} 423 424 {{< new-in "0.97.0" >}} `GroupByDate` accepts the same time layouts as in [time.Format](/functions/dateformat/) and The `.Key` in the result will be localized for the current language. 425 426 ### By Publish Date 427 428 {{< code file="layouts/partials/by-page-publish-date.html" >}} 429 <!-- Groups content by month according to the "publishDate" field in front matter --> 430 {{ range .Pages.GroupByPublishDate "2006-01" }} 431 <h3>{{ .Key }}</h3> 432 <ul> 433 {{ range .Pages }} 434 <li> 435 <a href="{{ .Permalink }}">{{ .Title }}</a> 436 <div class="meta">{{ .PublishDate.Format "Mon, Jan 2, 2006" }}</div> 437 </li> 438 {{ end }} 439 </ul> 440 {{ end }} 441 {{< /code >}} 442 443 {{< new-in "0.97.0" >}} `GroupByDate` accepts the same time layouts as in [time.Format](/functions/dateformat/) and The `.Key` in the result will be localized for the current language. 444 445 ### By Lastmod 446 447 {{< code file="layouts/partials/by-page-lastmod.html" >}} 448 <!-- Groups content by month according to the "lastMod" field in front matter --> 449 {{ range .Pages.GroupByLastmod "2006-01" }} 450 <h3>{{ .Key }}</h3> 451 <ul> 452 {{ range .Pages }} 453 <li> 454 <a href="{{ .Permalink }}">{{ .Title }}</a> 455 <div class="meta">{{ .Lastmod.Format "Mon, Jan 2, 2006" }}</div> 456 </li> 457 {{ end }} 458 </ul> 459 {{ end }} 460 {{< /code >}} 461 462 {{< new-in "0.97.0" >}} `GroupByDate` accepts the same time layouts as in [time.Format](/functions/dateformat/) and The `.Key` in the result will be localized for the current language. 463 464 ### By Expiry Date 465 466 {{< code file="layouts/partials/by-page-expiry-date.html" >}} 467 <!-- Groups content by month according to the "expiryDate" field in front matter --> 468 {{ range .Pages.GroupByExpiryDate "2006-01" }} 469 <h3>{{ .Key }}</h3> 470 <ul> 471 {{ range .Pages }} 472 <li> 473 <a href="{{ .Permalink }}">{{ .Title }}</a> 474 <div class="meta">{{ .ExpiryDate.Format "Mon, Jan 2, 2006" }}</div> 475 </li> 476 {{ end }} 477 </ul> 478 {{ end }} 479 {{< /code >}} 480 481 {{< new-in "0.97.0" >}} `GroupByDate` accepts the same time layouts as in [time.Format](/functions/dateformat/) and The `.Key` in the result will be localized for the current language. 482 483 ### By Page Parameter 484 485 {{< code file="layouts/partials/by-page-param.html" >}} 486 <!-- Groups content according to the "param_key" field in front matter --> 487 {{ range .Pages.GroupByParam "param_key" }} 488 <h3>{{ .Key }}</h3> 489 <ul> 490 {{ range .Pages }} 491 <li> 492 <a href="{{ .Permalink }}">{{ .Title }}</a> 493 <div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div> 494 </li> 495 {{ end }} 496 </ul> 497 {{ end }} 498 {{< /code >}} 499 500 ### By Page Parameter in Date Format 501 502 The following template takes grouping by `date` a step further and uses Go's layout string. See the [`Format` function][] for more examples of how to use Go's layout string to format dates in Hugo. 503 504 {{< code file="layouts/partials/by-page-param-as-date.html" >}} 505 <!-- Groups content by month according to the "param_key" field in front matter --> 506 {{ range .Pages.GroupByParamDate "param_key" "2006-01" }} 507 <h3>{{ .Key }}</h3> 508 <ul> 509 {{ range .Pages }} 510 <li> 511 <a href="{{ .Permalink }}">{{ .Title }}</a> 512 <div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div> 513 </li> 514 {{ end }} 515 </ul> 516 {{ end }} 517 {{< /code >}} 518 519 ### Reverse Key Order 520 521 Ordering of groups is performed by keys in alphanumeric order (A–Z, 1–100) and in reverse chronological order (i.e., with the newest first) for dates. 522 523 While these are logical defaults, they are not always the desired order. There are two different syntaxes to change Hugo's default ordering for groups, both of which work the same way. 524 525 #### 1. Adding the Reverse Method 526 527 ``` 528 {{ range (.Pages.GroupBy "Section").Reverse }} 529 ``` 530 531 ``` 532 {{ range (.Pages.GroupByDate "2006-01").Reverse }} 533 ``` 534 535 #### 2. Providing the Alternate Direction 536 537 ``` 538 {{ range .Pages.GroupByDate "2006-01" "asc" }} 539 ``` 540 541 ``` 542 {{ range .Pages.GroupBy "Section" "desc" }} 543 ``` 544 545 ### Order Within Groups 546 547 Because Grouping returns a `{{.Key}}` and a slice of pages, all of the ordering methods listed above are available. 548 549 Here is the ordering for the example that follows: 550 551 1. Content is grouped by month according to the `date` field in front matter. 552 2. Groups are listed in ascending order (i.e., the oldest groups first) 553 3. Pages within each respective group are ordered alphabetically according to the `title`. 554 555 {{< code file="layouts/partials/by-group-by-page.html" >}} 556 {{ range .Pages.GroupByDate "2006-01" "asc" }} 557 <h3>{{ .Key }}</h3> 558 <ul> 559 {{ range .Pages.ByTitle }} 560 <li> 561 <a href="{{ .Permalink }}">{{ .Title }}</a> 562 <div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div> 563 </li> 564 {{ end }} 565 </ul> 566 {{ end }} 567 {{< /code >}} 568 569 ## Filtering and Limiting Lists {#filtering-and-limiting-lists} 570 571 Sometimes you only want to list a subset of the available content. A 572 common is to only display posts from [**main sections**][mainsections] 573 on the blog's homepage. 574 575 See the documentation on [`where` function][wherefunction] and 576 [`first` function][firstfunction] for further details. 577 578 [base]: /templates/base/ 579 [bepsays]: https://bepsays.com/en/2016/12/19/hugo-018/ 580 [directorystructure]: /getting-started/directory-structure/ 581 [`Format` function]: /functions/format/ 582 [front matter]: /content-management/front-matter/ 583 [getpage]: /functions/getpage/ 584 [homepage]: /templates/homepage/ 585 [homepage]: /templates/homepage/ 586 [mentalmodel]: https://webstyleguide.com/wsg3/3-information-architecture/3-site-structure.html 587 [pagevars]: /variables/page/ 588 [partials]: /templates/partials/ 589 [RSS 2.0]: https://cyber.harvard.edu/rss/rss.html "RSS 2.0 Specification" 590 [rss]: /templates/rss/ 591 [sections]: /content-management/sections/ 592 [sectiontemps]: /templates/section-templates/ 593 [sitevars]: /variables/site/ 594 [taxlists]: /templates/taxonomy-templates/#taxonomy-list-templates 595 [taxterms]: /templates/taxonomy-templates/#taxonomy-terms-templates 596 [taxvars]: /variables/taxonomy/ 597 [views]: /templates/views/ 598 [wherefunction]: /functions/where/ 599 [firstfunction]: /functions/first/ 600 [mainsections]: /functions/where/#mainsections