github.com/rabbouni145/gg@v0.47.1/docs/content/en/functions/intersect.md (about) 1 --- 2 title: intersect 3 linktitle: intersect 4 description: Returns the common elements of two arrays or slices. 5 godocref: 6 date: 2017-02-01 7 publishdate: 2017-02-01 8 lastmod: 2017-02-01 9 categories: [functions] 10 menu: 11 docs: 12 parent: "functions" 13 keywords: [] 14 signature: ["intersect SET1 SET2"] 15 workson: [] 16 hugoversion: 17 relatedfuncs: [] 18 deprecated: false 19 aliases: [] 20 --- 21 22 The elements supported are strings, integers, and floats (only float64). 23 24 A useful example of `intersect` functionality is a "related posts" block. `isset` allows us to create a list of links to other posts that have tags that intersect with the tags in the current post. 25 26 The following is an example of a "related posts" [partial template][partials] that could be added to a [single page template][single]: 27 28 {{< code file="layouts/partials/related-posts.html" download="related-posts.html" >}} 29 <ul> 30 {{ $page_link := .Permalink }} 31 {{ $tags := .Params.tags }} 32 {{ range .Site.Pages }} 33 {{ $page := . }} 34 {{ $has_common_tags := intersect $tags .Params.tags | len | lt 0 }} 35 {{ if and $has_common_tags (ne $page_link $page.Permalink) }} 36 <li><a href="{{ $page.Permalink }}">{{ $page.Title }}</a></li> 37 {{ end }} 38 {{ end }} 39 </ul> 40 {{< /code >}} 41 42 This is also very useful to use as `AND` filters when combined with where: 43 44 ``` 45 {{ $pages := where .Site.RegularPages "Type" "not in" (slice "page" "about") }} 46 {{ $pages := $pages | union (where .Site.RegularPages "Params.pinned" true) }} 47 {{ $pages := $pages | intersect (where .Site.RegularPages "Params.images" "!=" nil) }} 48 ``` 49 50 The above fetches regular pages not of `page` or `about` type unless they are pinned. And finally, we exclude all pages with no `images` set in Page params. 51 52 See [union](/functions/union) for `OR`. 53 54 55 [partials]: /templates/partials/ 56 [single]: /templates/single-page-templates/