From 80e833c5e9adceeda63fc4b867ece9f38b9f969b Mon Sep 17 00:00:00 2001 From: George Cushen Date: Sun, 24 Sep 2017 23:00:25 +0100 Subject: [PATCH] posts widget: Add filter to exclude tags (Close #170) * Change `tags` option to `tags_include` * Add `tags_exclude` option --- exampleSite/content/home/posts.md | 7 ++++-- layouts/partials/widgets/posts.html | 34 ++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/exampleSite/content/home/posts.md b/exampleSite/content/home/posts.md index a341b925..d23ef205 100644 --- a/exampleSite/content/home/posts.md +++ b/exampleSite/content/home/posts.md @@ -12,8 +12,11 @@ widget = "posts" # Order that this section will appear in. weight = 40 -# Show posts that contain the following tags. Default to any tags. -tags = [] +# Filter posts by tag. +# By default, show all recent posts. +# Filtering example: `tags_include = ["hugo", "academic"]` +tags_include = [] +tags_exclude = [] # Number of posts to list. count = 5 diff --git a/layouts/partials/widgets/posts.html b/layouts/partials/widgets/posts.html index 181d8e3a..4e9ef090 100644 --- a/layouts/partials/widgets/posts.html +++ b/layouts/partials/widgets/posts.html @@ -22,16 +22,34 @@ {{ with $page.Content }}

{{ . | markdownify }}

{{ end }} - {{ if $page.Params.tags }} - {{ range first $page.Params.count (where (where (where $.Data.Pages "Type" "post") ".Params.tags" "intersect" $page.Params.tags) ".Params.notonhomepage" nil) }} + {{ $posts := where (where $.Data.Pages "Type" "post") ".Params.notonhomepage" nil }} + {{ if $page.Params.tags_include }} + {{ $posts := where (where (where $.Data.Pages "Type" "post") ".Params.tags" "intersect" $page.Params.tags_include) ".Params.notonhomepage" nil }} + {{ end }} + + {{ $.Scratch.Add "show_post" "1" }} + {{ range $post := first $page.Params.count $posts }} + + {{ $.Scratch.Set "show_post" "1" }} + + {{/* If `tags_include` is set, exclude posts with no tags. */}} + {{ if and ($page.Params.tags_include) (lt (len .Params.tags) 1) }} + {{ $.Scratch.Set "show_post" "0" }} + {{end}} + + {{/* If `tags_exclude` is set, exclude posts. */}} + {{ range $key, $val := .Params.tags }} + {{ if in $page.Params.tags_exclude $val }} + {{ $.Scratch.Set "show_post" "0" }} + {{end}} + {{end}} + + {{ $show_post := $.Scratch.Get "show_post" }} + {{ if ne $show_post "0" }} {{ $params := dict "post" . "page" $page }} {{ partial "post_li" $params }} - {{ end }} - {{ else }} - {{ range first $page.Params.count (where (where $.Data.Pages "Type" "post") ".Params.notonhomepage" nil) }} - {{ $params := dict "post" . "page" $page }} - {{ partial "post_li" $params }} - {{ end }} + {{end}} + {{ end }}