github.com/rabbouni145/gg@v0.47.1/docs/content/en/functions/len.md (about) 1 --- 2 title: len 3 linktitle: len 4 description: Returns the length of a variable according to its type. 5 godocref: https://golang.org/pkg/builtin/#len 6 date: 2017-02-01 7 publishdate: 2017-02-01 8 lastmod: 2017-04-18 9 categories: [functions] 10 keywords: [] 11 signature: ["len INPUT"] 12 workson: [lists,taxonomies,terms] 13 hugoversion: 14 relatedfuncs: [] 15 deprecated: false 16 toc: false 17 aliases: [] 18 --- 19 20 `len` is a built-in function in Go that returns the length of a variable according to its type. From the Go documentation: 21 22 > Array: the number of elements in v. 23 > 24 > Pointer to array: the number of elements in *v (even if v is nil). 25 > 26 > Slice, or map: the number of elements in v; if v is nil, len(v) is zero. 27 > 28 > String: the number of bytes in v. 29 > 30 > Channel: the number of elements queued (unread) in the channel buffer; if v is nil, len(v) is zero. 31 32 `len` is also considered a [fundamental function for Hugo templating][]. 33 34 ## `len` Example 1: Longer Headings 35 36 You may want to append a class to a heading according to the length of the string therein. The following templating checks to see if the title's length is greater than 80 characters and, if so, adds a `long-title` class to the `<h1>`: 37 38 {{< code file="check-title-length.html" >}} 39 <header> 40 <h1{{if gt (len .Title) 80}} class="long-title"{{end}}>{{.Title}}</h1> 41 </header> 42 {{< /code >}} 43 44 ## `len` Example 2: Counting Pages with `where` 45 46 The following templating uses [`where`][] in conjunction with `len` to figure out the total number of content pages in a `posts` [section][]: 47 48 {{< code file="how-many-posts.html" >}} 49 {{ $posts := (where .Site.RegularPages "Section" "==" "post") }} 50 {{ $postCount := len $posts }} 51 {{< /code >}} 52 53 Note the use of `.RegularPages`, a [site variable][] that counts all regular content pages but not the `_index.md` pages used to add front matter and content to [list templates][]. 54 55 56 [fundamental function for Hugo templating]: /templates/introduction/ 57 [list templates]: /templates/lists/ 58 [section]: /content-management/sections/ 59 [site variable]: /variables/site/ 60 [`where`]: /functions/where/