Debian Codegraph @

docs/content/en/methods/site/MainSections.md

raw
1 ---
2 title: MainSections
3 description: Returns a slice of the main section names as defined in your project configuration, falling back to the top-level section with the most pages.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 returnType: '[]string'
9 signatures: [SITE.MainSections]
10 ---
11
12 Project configuration:
13
14 {{< code-toggle file=hugo >}}
15 mainSections = ['books','films']
16 {{< /code-toggle >}}
17
18 Template:
19
20 ```go-html-template
21 {{ .Site.MainSections }} → [books films]
22 ```
23
24 If `mainSections` is not defined in your project configuration, this method returns a slice with one element---the top-level section with the most pages.
25
26 With this content structure, the `films` section has the most pages:
27
28 ```text
29 content/
30 ├── books/
31 │ ├── book-1.md
32 │ └── book-2.md
33 ├── films/
34 │ ├── film-1.md
35 │ ├── film-2.md
36 │ └── film-3.md
37 └── _index.md
38 ```
39
40 Template:
41
42 ```go-html-template
43 {{ .Site.MainSections }} → [films]
44 ```
45
46 When creating a theme, instead of hardcoding section names when listing the most relevant pages on the front page, instruct users to set `mainSections` in their project configuration.
47
48 Then your _home_ template can do something like this:
49
50 ```go-html-template {file="layouts/home.html"}
51 {{ range where .Site.RegularPages "Section" "in" .Site.MainSections }}
52 <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
53 {{ end }}
54 ```