hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

pagination.md (5473B)

    1 ---
    2 title: Pagination
    3 linktitle: Pagination
    4 description: Hugo supports pagination for your homepage, section pages, and taxonomies.
    5 date: 2017-02-01
    6 publishdate: 2017-02-01
    7 lastmod: 2017-02-01
    8 categories: [templates]
    9 keywords: [lists,sections,pagination]
   10 menu:
   11   docs:
   12     parent: "templates"
   13     weight: 140
   14 weight: 140
   15 sections_weight: 140
   16 draft: false
   17 aliases: [/extras/pagination,/doc/pagination/]
   18 toc: true
   19 ---
   20 
   21 The real power of Hugo pagination shines when combined with the [`where` function][where] and its SQL-like operators: [`first`][], [`last`][], and [`after`][]. You can even [order the content][lists] the way you've become used to with Hugo.
   22 
   23 ## Configure Pagination
   24 
   25 Pagination can be configured in your [site configuration][configuration]:
   26 
   27 `paginate`
   28 : default = `10`. This setting can be overridden within the template.
   29 
   30 `paginatePath`
   31 : default = `page`. Allows you to set a different path for your pagination pages.
   32 
   33 Setting `paginate` to a positive value will split the list pages for the homepage, sections and taxonomies into chunks of that size. But note that the generation of the pagination pages for sections, taxonomies and homepage is *lazy* --- the pages will not be created if not referenced by a `.Paginator` (see below).
   34 
   35 `paginatePath` is used to adapt the `URL` to the pages in the paginator (the default setting will produce URLs on the form `/page/1/`.
   36 
   37 ## List Paginator Pages
   38 
   39 {{% warning %}}
   40 `.Paginator` is provided to help you build a pager menu. This feature is currently only supported on homepage and list pages (i.e., taxonomies and section lists).
   41 {{% /warning %}}
   42 
   43 There are two ways to configure and use a `.Paginator`:
   44 
   45 1. The simplest way is just to call `.Paginator.Pages` from a template. It will contain the pages for *that page*.
   46 2. Select another set of pages with the available template functions and ordering options, and pass the slice to `.Paginate`, e.g.
   47   * `{{ range (.Paginate ( first 50 .Pages.ByTitle )).Pages }}` or
   48   * `{{ range (.Paginate .RegularPagesRecursive).Pages }}`.
   49 
   50 For a given **Page**, it's one of the options above. The `.Paginator` is static and cannot change once created.
   51 
   52 If you call `.Paginator` or `.Paginate` multiple times on the same page, you should ensure all the calls are identical. Once *either* `.Paginator` or `.Paginate` is called while generating a page, its result is cached, and any subsequent similar call will reuse the cached result. This means that any such calls which do not match the first one will not behave as written.
   53 
   54 (Remember that function arguments are eagerly evaluated, so a call like `$paginator := cond x .Paginator (.Paginate .RegularPagesRecursive)` is an example of what you should *not* do. Use `if`/`else` instead to ensure exactly one evaluation.)
   55 
   56 The global page size setting (`Paginate`) can be overridden by providing a positive integer as the last argument. The examples below will give five items per page:
   57 
   58 * `{{ range (.Paginator 5).Pages }}`
   59 * `{{ $paginator := .Paginate (where .Pages "Type" "posts") 5 }}`
   60 
   61 It is also possible to use the `GroupBy` functions in combination with pagination:
   62 
   63 ```
   64 {{ range (.Paginate (.Pages.GroupByDate "2006")).PageGroups  }}
   65 ```
   66 
   67 ## Build the navigation
   68 
   69 The `.Paginator` contains enough information to build a paginator interface.
   70 
   71 The easiest way to add this to your pages is to include the built-in template (with `Bootstrap`-compatible styles):
   72 
   73 ```
   74 {{ template "_internal/pagination.html" . }}
   75 ```
   76 
   77 {{% note "When to Create `.Paginator`" %}}
   78 If you use any filters or ordering functions to create your `.Paginator` *and* you want the navigation buttons to be shown before the page listing, you must create the `.Paginator` before it's used.
   79 {{% /note %}}
   80 
   81 The following example shows how to create `.Paginator` before its used:
   82 
   83 ```
   84 {{ $paginator := .Paginate (where .Pages "Type" "posts") }}
   85 {{ template "_internal/pagination.html" . }}
   86 {{ range $paginator.Pages }}
   87    {{ .Title }}
   88 {{ end }}
   89 ```
   90 
   91 Without the `where` filter, the above example is even simpler:
   92 
   93 ```
   94 {{ template "_internal/pagination.html" . }}
   95 {{ range .Paginator.Pages }}
   96    {{ .Title }}
   97 {{ end }}
   98 ```
   99 
  100 If you want to build custom navigation, you can do so using the `.Paginator` object, which includes the following properties:
  101 
  102 `PageNumber`
  103 : The current page's number in the pager sequence
  104 
  105 `URL`
  106 : The relative URL to the current pager
  107 
  108 `Pages`
  109 : The pages in the current pager
  110 
  111 `NumberOfElements`
  112 : The number of elements on this page
  113 
  114 `HasPrev`
  115 : Whether there are page(s) before the current
  116 
  117 `Prev`
  118 : The pager for the previous page
  119 
  120 `HasNext`
  121 : Whether there are page(s) after the current
  122 
  123 `Next`
  124 : The pager for the next page
  125 
  126 `First`
  127 : The pager for the first page
  128 
  129 `Last`
  130 : The pager for the last page
  131 
  132 `Pagers`
  133 : A list of pagers that can be used to build a pagination menu
  134 
  135 `PageSize`
  136 : Size of each pager
  137 
  138 `TotalPages`
  139 : The number of pages in the paginator
  140 
  141 `TotalNumberOfElements`
  142 : The number of elements on all pages in this paginator
  143 
  144 ## Additional information
  145 
  146 The pages are built on the following form (`BLANK` means no value):
  147 
  148 ```
  149 [SECTION/TAXONOMY/BLANK]/index.html
  150 [SECTION/TAXONOMY/BLANK]/page/1/index.html => redirect to  [SECTION/TAXONOMY/BLANK]/index.html
  151 [SECTION/TAXONOMY/BLANK]/page/2/index.html
  152 ....
  153 ```
  154 
  155 
  156 [`first`]: /functions/first/
  157 [`last`]: /functions/last/
  158 [`after`]: /functions/after/
  159 [configuration]: /getting-started/configuration/
  160 [lists]: /templates/lists/
  161 [where]: /functions/where/