collections.Complement
语法
collections.Complement COLLECTION [COLLECTION...]
返回
any
别名
complement
查找 $c3
中不存在于 $c1
或 $c2
中的元素
{{ $c1 := slice 3 }}
{{ $c2 := slice 4 5 }}
{{ $c3 := slice 1 2 3 4 5 }}
{{ complement $c1 $c2 $c3 }} → [1 2]
{{ $c3 | complement $c1 $c2 }} → [1 2]
您还可以将 complement
函数与页面集合一起使用。假设您的站点有五种内容类型
content/
├── blog/
├── books/
├── faqs/
├── films/
└── songs/
列出除博客文章 (blog
) 和常见问题 (faqs
) 之外的所有内容
{{ $blog := where site.RegularPages "Type" "blog" }}
{{ $faqs := where site.RegularPages "Type" "faqs" }}
{{ range site.RegularPages | complement $blog $faqs }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
{{ range where site.RegularPages "Type" "not in" (slice "blog" "faqs") }}
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
{{ end }}
在此示例中,我们使用 complement
函数从句子中删除停用词
{{ $text := "The quick brown fox jumps over the lazy dog" }}
{{ $stopWords := slice "a" "an" "in" "over" "the" "under" }}
{{ $filtered := split $text " " | complement $stopWords }}
{{ delimit $filtered " " }} → The quick brown fox jumps lazy dog