You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

section.md 7.6KB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. +++
  2. title = "Section"
  3. weight = 20
  4. +++
  5. A section is created whenever a directory (or subdirectory) in the `content` section contains an
  6. `_index.md` file. If a directory does not contain an `_index.md` file, no section will be
  7. created, but Markdown files within that directory will still create pages (known as orphan pages).
  8. The index page (i.e., the page displayed when a user browses to your `base_url`) is a section,
  9. which is created whether or not you add an `_index.md` file at the root of your `content` directory.
  10. If you do not create an `_index.md` file in your content directory, this main content section will
  11. not have any content or metadata. If you would like to add content or metadata, you can add an
  12. `_index.md` file at the root of the `content` directory and edit it just as you would edit any other
  13. `_index.md` file; your `index.html` template will then have access to that content and metadata.
  14. Any non-Markdown file in a section directory is added to the `assets` collection of the section, as explained in the
  15. [content overview](@/documentation/content/overview.md#asset-colocation). These files are then available in the
  16. Markdown file using relative links.
  17. ## Front matter
  18. The `_index.md` file within a directory defines the content and metadata for that section. To set
  19. the metadata, add front matter to the file.
  20. The TOML front matter is a set of metadata embedded in a file at the beginning of the file enclosed by triple pluses (`+++`).
  21. After the closing `+++`, you can add content, which will be parsed as Markdown and made available
  22. to your templates through the `section.content` variable.
  23. Although none of the front matter variables are mandatory, the opening and closing `+++` are required.
  24. Here is an example `_index.md` with all the available variables. The values provided below are the
  25. default values.
  26. ```toml
  27. title = ""
  28. description = ""
  29. # Used to sort pages by "date", "weight" or "none". See below for more information.
  30. sort_by = "none"
  31. # Used by the parent section to order its subsections.
  32. # Lower values have higher priority.
  33. weight = 0
  34. # Template to use to render this section page.
  35. template = "section.html"
  36. # The given template is applied to ALL pages below the section, recursively.
  37. # If you have several nested sections, each with a page_template set, the page
  38. # will always use the closest to itself.
  39. # However, a page's own `template` variable will always have priority.
  40. # Not set by default.
  41. page_template =
  42. # This sets the number of pages to be displayed per paginated page.
  43. # No pagination will happen if this isn't set or if the value is 0.
  44. paginate_by = 0
  45. # If set, this will be the path used by the paginated page. The page number will be appended after this path.
  46. # The default is page/1.
  47. paginate_path = "page"
  48. # This determines whether to insert a link for each header like the ones you can see on this site if you hover over
  49. # a header.
  50. # The default template can be overridden by creating an `anchor-link.html` file in the `templates` directory.
  51. # This value can be "left", "right" or "none".
  52. insert_anchor_links = "none"
  53. # If set to "true", the section pages will be in the search index. This is only used if
  54. # `build_search_index` is set to "true" in the Zola configuration file.
  55. in_search_index = true
  56. # If set to "true", the section homepage is rendered.
  57. # Useful when the section is used to organize pages (not used directly).
  58. render = true
  59. # This determines whether to redirect when a user lands on the section. Defaults to not being set.
  60. # Useful for the same reason as `render` but when you don't want a 404 when
  61. # landing on the root section page.
  62. # Example: redirect_to = "documentation/content/overview"
  63. redirect_to = ""
  64. # If set to "true", the section will pass its pages on to the parent section. Defaults to `false`.
  65. # Useful when the section shouldn't split up the parent section, like
  66. # sections for each year under a posts section.
  67. transparent = false
  68. # Use aliases if you are moving content but want to redirect previous URLs to the
  69. # current one. This takes an array of paths, not URLs.
  70. aliases = []
  71. # Your own data.
  72. [extra]
  73. ```
  74. Keep in mind that any configuration options apply only to the direct pages, not to the subsections' pages.
  75. ## Pagination
  76. To enable pagination for a section's pages, set `paginate_by` to a positive number. See
  77. [pagination template documentation](@/documentation/templates/pagination.md) for more information
  78. on what variables are available in the template.
  79. You can also change the pagination path (the word displayed while paginated in the URL, like `page/1`)
  80. by setting the `paginate_path` variable, which defaults to `page`.
  81. ## Sorting
  82. It is very common for Zola templates to iterate over pages or sections
  83. to display all pages/sections in a given directory. Consider a very simple
  84. example: a `blog` directory with three files: `blog/Post_1.md`,
  85. `blog/Post_2.md` and `blog/Post_3.md`. To iterate over these posts and
  86. create a list of links to the posts, a simple template might look like this:
  87. ```j2
  88. {% for post in section.pages %}
  89. <h1><a href="{{ post.permalink }}">{{ post.title }}</a></h1>
  90. {% endfor %}
  91. ```
  92. This would iterate over the posts in the order specified
  93. by the `sort_by` variable set in the `_index.md` page for the corresponding
  94. section. The `sort_by` variable can be given one of three values: `date`,
  95. `weight` or `none`. If `sort_by` is not set, the pages will be
  96. sorted in the `none` order, which is not intended for sorted content.
  97. Any page that is missing the data it needs to be sorted will be ignored and
  98. won't be rendered. For example, if a page is missing the date variable and its
  99. section sets `sort_by = "date"`, then that page will be ignored.
  100. The terminal will warn you if this occurs.
  101. If several pages have the same date/weight/order, their permalink will be used
  102. to break the tie based on alphabetical order.
  103. ## Sorting pages
  104. The `sort_by` front-matter variable can have the following values:
  105. ### `date`
  106. This will sort all pages by their `date` field, from the most recent (at the
  107. top of the list) to the oldest (at the bottom of the list). Each page will
  108. get `page.earlier` and `page.later` variables that contain the pages with
  109. earlier and later dates, respectively.
  110. ### `weight`
  111. This will be sort all pages by their `weight` field, from lightest weight
  112. (at the top of the list) to heaviest (at the bottom of the list). Each
  113. page gets `page.lighter` and `page.heavier` variables that contain the
  114. pages with lighter and heavier weights, respectively.
  115. When iterating through pages, you may wish to use the Tera `reverse` filter,
  116. which reverses the order of the pages. For example, after using the `reverse` filter,
  117. pages sorted by weight will be sorted from lightest (at the top) to heaviest
  118. (at the bottom); pages sorted by date will be sorted from oldest (at the top)
  119. to newest (at the bottom).
  120. `reverse` has no effect on `page.later`/`page.earlier` or `page.heavier`/`page.lighter`.
  121. ## Sorting subsections
  122. Sorting sections is a bit less flexible: sections are always sorted by `weight`,
  123. and do not have variables that point to the heavier/lighter sections.
  124. By default, the lightest (lowest `weight`) subsections will be at
  125. the top of the list and the heaviest (highest `weight`) will be at the bottom;
  126. the `reverse` filter reverses this order.
  127. **Note**: Unlike pages, permalinks will **not** be used to break ties between
  128. equally weighted sections. Thus, if the `weight` variable for your section is not set (or if it
  129. is set in a way that produces ties), then your sections will be sorted in
  130. **random** order. Moreover, that order is determined at build time and will
  131. change with each site rebuild. Thus, if there is any chance that you will
  132. iterate over your sections, you should always assign them a weight.