github.com/rabbouni145/gg@v0.47.1/docs/content/en/templates/lists.md (about) 1 --- 2 title: Lists of Content in Hugo 3 linktitle: List Page 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) 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 | ├── post 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/post/_index.md`: 77 78 {{< code file="content/post/_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/post/*.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/post/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="/post/post-01/">Post 1</a></li> 128 <li><a href="/post/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](http://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/post.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/post/*.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 ### By Publish Date 425 426 {{< code file="layouts/partials/by-page-publish-date.html" >}} 427 <!-- Groups content by month according to the "publishdate" field in front matter --> 428 {{ range .Pages.GroupByPublishDate "2006-01" }} 429 <h3>{{ .Key }}</h3> 430 <ul> 431 {{ range .Pages }} 432 <li> 433 <a href="{{ .Permalink }}">{{ .Title }}</a> 434 <div class="meta">{{ .PublishDate.Format "Mon, Jan 2, 2006" }}</div> 435 </li> 436 {{ end }} 437 </ul> 438 {{ end }} 439 {{< /code >}} 440 441 ### By Page Parameter 442 443 {{< code file="layouts/partials/by-page-param.html" >}} 444 <!-- Groups content according to the "param_key" field in front matter --> 445 {{ range .Pages.GroupByParam "param_key" }} 446 <h3>{{ .Key }}</h3> 447 <ul> 448 {{ range .Pages }} 449 <li> 450 <a href="{{ .Permalink }}">{{ .Title }}</a> 451 <div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div> 452 </li> 453 {{ end }} 454 </ul> 455 {{ end }} 456 {{< /code >}} 457 458 ### By Page Parameter in Date Format 459 460 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. 461 462 {{< code file="layouts/partials/by-page-param-as-date.html" >}} 463 <!-- Groups content by month according to the "param_key" field in front matter --> 464 {{ range .Pages.GroupByParamDate "param_key" "2006-01" }} 465 <h3>{{ .Key }}</h3> 466 <ul> 467 {{ range .Pages }} 468 <li> 469 <a href="{{ .Permalink }}">{{ .Title }}</a> 470 <div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div> 471 </li> 472 {{ end }} 473 </ul> 474 {{ end }} 475 {{< /code >}} 476 477 ### Reverse Key Order 478 479 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. 480 481 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. 482 483 #### 1. Adding the Reverse Method 484 485 ``` 486 {{ range (.Pages.GroupBy "Section").Reverse }} 487 ``` 488 489 ``` 490 {{ range (.Pages.GroupByDate "2006-01").Reverse }} 491 ``` 492 493 #### 2. Providing the Alternate Direction 494 495 ``` 496 {{ range .Pages.GroupByDate "2006-01" "asc" }} 497 ``` 498 499 ``` 500 {{ range .Pages.GroupBy "Section" "desc" }} 501 ``` 502 503 ### Order Within Groups 504 505 Because Grouping returns a `{{.Key}}` and a slice of pages, all of the ordering methods listed above are available. 506 507 Here is the ordering for the example that follows: 508 509 1. Content is grouped by month according to the `date` field in front matter. 510 2. Groups are listed in ascending order (i.e., the oldest groups first) 511 3. Pages within each respective group are ordered alphabetically according to the `title`. 512 513 {{< code file="layouts/partials/by-group-by-page.html" >}} 514 {{ range .Pages.GroupByDate "2006-01" "asc" }} 515 <h3>{{ .Key }}</h3> 516 <ul> 517 {{ range .Pages.ByTitle }} 518 <li> 519 <a href="{{ .Permalink }}">{{ .Title }}</a> 520 <div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div> 521 </li> 522 {{ end }} 523 </ul> 524 {{ end }} 525 {{< /code >}} 526 527 ## Filter and Limiting Lists 528 529 Sometimes you only want to list a subset of the available content. A common is to only display “Posts” on blog's homepage. You can accomplish this with the `where` function. 530 531 ### `where` 532 533 `where` works in a similar manner to the [`where` keyword in SQL][wherekeyword]. It selects all elements of the array or slice that match the provided field and value. `where` takes three arguments: 534 535 1. `array` *or* `slice of maps or structs` 536 2. `key` *or* `field name` 537 3. `match value` 538 539 {{< code file="layouts/_default/index.html" >}} 540 {{ range where .Pages "Section" "post" }} 541 {{ .Content }} 542 {{ end }} 543 {{< /code >}} 544 545 You can see more examples in the [functions documentation for `where`][wherefunction]. 546 547 ### `first` 548 549 `first` works in a similar manner to the [`limit` keyword in SQL][limitkeyword]. It reduces the array to only the `first N` elements. It takes the array and number of elements as input. `first` takes two arguments: 550 551 1. `array` *or* `slice of maps or structs` 552 2. `number of elements` 553 554 {{< code file="layout/_default/section.html" >}} 555 {{ range first 10 .Pages }} 556 {{ .Render "summary" }} 557 {{ end }} 558 {{< /code >}} 559 560 ### `first` and `where` Together 561 562 Using `first` and `where` together can be very powerful: 563 564 {{< code file="first-and-where-together.html" >}} 565 <!-- Orders the content inside the "posts" section by the "title" field and then ranges through only the first 5 posts --> 566 {{ range first 5 (where .Pages "Section" "post").ByTitle }} 567 {{ .Content }} 568 {{ end }} 569 {{< /code >}} 570 571 [base]: /templates/base/ 572 [bepsays]: http://bepsays.com/en/2016/12/19/hugo-018/ 573 [directorystructure]: /getting-started/directory-structure/ 574 [`Format` function]: /functions/format/ 575 [front matter]: /content-management/front-matter/ 576 [getpage]: /functions/getpage/ 577 [homepage]: /templates/homepage/ 578 [homepage]: /templates/homepage/ 579 [limitkeyword]: https://www.techonthenet.com/sql/select_limit.php 580 [mentalmodel]: http://webstyleguide.com/wsg3/3-information-architecture/3-site-structure.html 581 [pagevars]: /variables/page/ 582 [partials]: /templates/partials/ 583 [RSS 2.0]: http://cyber.law.harvard.edu/rss/rss.html "RSS 2.0 Specification" 584 [rss]: /templates/rss/ 585 [sections]: /content-management/sections/ 586 [sectiontemps]: /templates/section-templates/ 587 [sitevars]: /variables/site/ 588 [taxlists]: /templates/taxonomy-templates/#taxonomy-list-templates/ 589 [taxterms]: /templates/taxonomy-templates/#taxonomy-terms-templates/ 590 [taxvars]: /variables/taxonomy/ 591 [views]: /templates/views/ 592 [wherefunction]: /functions/where/ 593 [wherekeyword]: https://www.techonthenet.com/sql/where.php