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.5KB

6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. +++
  2. title = "Section"
  3. weight = 20
  4. +++
  5. A section is created whenever a folder (or subfolder) in the `content` section contains an
  6. `_index.md` file. If a folder does not contain an `_index.md` file, no section will be
  7. created, but markdown files within that folder 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` folder.
  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` folder 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 the section folder is added to the `assets` collection of the section, as explained in the [Content Overview](./documentation/content/overview.md#assets-colocation). These files are then available from the Markdown using relative links.
  15. ## Front-matter
  16. The `_index.md` file within a folder defines the content and metadata for that section. To set
  17. the metadata, add front matter to the file.
  18. The front-matter is a set of metadata embedded in a file. In Zola,
  19. it is at the beginning of the file, surrounded by `+++` and uses TOML.
  20. After the closing `+++`, you can add content that will be parsed as markdown and will be available
  21. to your templates through the `section.content` variable.
  22. While none of the front-matter variables are mandatory, the opening and closing `+++` are required.
  23. Here is an example `_index.md` with all the variables available. The values provided below are the
  24. default values.
  25. ```md
  26. +++
  27. title = ""
  28. description = ""
  29. # Whether to sort pages by "date", "weight", or "none". More on that below
  30. sort_by = "none"
  31. # Used by the parent section to order its subsections.
  32. # Lower values have priority.
  33. weight = 0
  34. # Template to use to render this section page
  35. template = "section.html"
  36. # Apply the given template 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 own `template` variable will always have priority.
  40. # Not set by default
  41. page_template =
  42. # How many 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, will be the path used by paginated page and the page number will be appended after it.
  46. # For example the default would be page/1
  47. paginate_path = "page"
  48. # Whether to insert a link for each header like the ones you can see in this site if you hover one
  49. # The default template can be overridden by creating a `anchor-link.html` in the `templates` directory
  50. # Options are "left", "right" and "none"
  51. insert_anchor_links = "none"
  52. # Whether the section pages should be in the search index. This is only used if
  53. # `build_search_index` is set to true in the config
  54. in_search_index = true
  55. # Whether to render that section homepage or not.
  56. # Useful when the section is only there to organize things but is not meant
  57. # to be used directly
  58. render = true
  59. # Whether to redirect when landing on that 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. # Whether the section should 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. # Your own data
  69. [extra]
  70. +++
  71. Some content
  72. ```
  73. Keep in mind that any configuration apply only to the direct pages, not to the subsections' pages.
  74. ## Pagination
  75. To enable pagination for a section's pages, simply set `paginate_by` to a positive number and it will automatically
  76. paginate by this much. See [pagination template documentation](./documentation/templates/pagination.md) for more information
  77. on what will be available in the template.
  78. You can also change the pagination path (the word displayed while paginated in the URL, like `page/1`)
  79. by setting the `paginate_path` variable, which defaults to `page`.
  80. ## Sorting
  81. It is very common for Zola templates to iterate over pages or sections
  82. to display all pages/sections a given directory. Consider a very simple
  83. example: a `blog` directory with three files: `blog/Post_1.md`,
  84. `blog/Post_2.md`, and `blog/Post_3.md`. To iterate over these posts and
  85. create a list of links to the posts, a simple template might look like this:
  86. ```j2
  87. {% for post in section.pages %}
  88. <h1><a href="{{ post.permalink }}">{{ post.title }}</a></h1>
  89. {% endfor %}
  90. ```
  91. This would iterate over the posts, and would do so in a specific order
  92. based on the `sort_by` variable set in the `_index.md` page for the
  93. containing section. The `sort_by` variable can be given three values: `date`,
  94. `weight`, and `none`. If no `sort_by` method is set, the pages will be
  95. sorted in the `none` order, which is not intended to be used for sorted content.
  96. Any page that is missing the data it needs to be sorted will be ignored and
  97. won't be rendered. For example, if a page is missing the date variable the
  98. containing section sets `sort_by = "date"`, then that page will be ignored.
  99. The terminal will warn you if this is happening.
  100. If several pages have the same date/weight/order, their permalink will be used
  101. to break the tie following an alphabetical order.
  102. ## Sorting Pages
  103. The `sort_by` front-matter variable can have the following values:
  104. ### `date`
  105. This will sort all pages by their `date` field, from the most recent (at the
  106. top of the list) to the oldest (at the bottom of the list). Each page will
  107. get `page.earlier` and `page.later` variables that contain the pages with
  108. earlier and later dates, respectively.
  109. ### `weight`
  110. This will be sort all pages by their `weight` field, from lightest weight
  111. (at the top of the list) to heaviest (at the bottom of the list). Each
  112. page gets `page.lighter` and `page.heavier` variables that contain the
  113. pages with lighter and heavier weights, respectively.
  114. When iterating through pages, you may wish to use the Tera `reverse` filter,
  115. which reverses the order of the pages. Thus, after using the `reverse` filter,
  116. pages sorted by weight will be sorted from lightest (at the top) to heaviest
  117. (at the bottom); pages sorted by date will be sorted from oldest (at the top)
  118. to newest (at the bottom).
  119. `reverse` has no effect on `page.later`/`page.earlier`/`page.heavier`/`page.lighter`.
  120. ## Sorting Subsections
  121. Sorting sections is a bit less flexible: sections are always sorted by `weight`,
  122. and do not have any variables that point to the next heavier/lighter sections.
  123. Based on this, by default the lightest (lowest `weight`) subsections will be at
  124. the top of the list and the heaviest (highest `weight`) will be at the top;
  125. the `reverse` filter reverses this order.
  126. **Note**: Unlike pages, permalinks will **not** be used to break ties between
  127. equally weighted sections. Thus, if the `weight` variable for your section is not set (or if it
  128. is set in a way that produces ties), then your sections will be sorted in
  129. **random** order. Moreover, that order is determined at build time and will
  130. change with each site rebuild. Thus, if there is any chance that you will
  131. iterate over your sections, you should always assign them weight.