collections.Intersect
一個有用的示例是將其與 where
配合使用,作為 AND
篩選器:
{{ $pages := where .Site.RegularPages "Type" "not in" (slice "page" "about") }}
{{ $pages := $pages | union (where .Site.RegularPages "Params.pinned" true) }}
{{ $pages := $pages | intersect (where .Site.RegularPages "Params.images" "!=" nil) }}
上述代碼將選取常規頁面,排除 page
或 about
類型,除非它們是被釘選的。最後,我們排除了所有在頁面參數中沒有設置 images
的頁面。
查閱 union 以瞭解如何實現 OR
。