feat(widget): restrict post filter to only one tag or category

Close #815

BREAKING CHANGES

Remove `tags_include` and `tags_exclude` from `home/posts.md`
Add `filter_tag` and `filter_category` to `home/posts.md`
This commit is contained in:
George Cushen committed 2018-12-16 00:48:51 +00:00
1 parent 5c0c7f5def
commit fbcb7f2761
2 files changed
+33 -82

No files matched your search

+9 -12
View File
@@ -1,23 +1,15 @@
+++ +++
# Recent Posts widget. # Recent Posts widget.
# This widget displays recent posts from `content/post/`. # This widget displays recent posts from `content/post/`.
widget = "posts" widget = "posts" # Do not modify this line!
active = true active = true # Activate this widget? true/false
weight = 40 # Order that this section will appear.
date = 2016-04-20T00:00:00 date = 2016-04-20T00:00:00
title = "Recent Posts" title = "Recent Posts"
subtitle = "" subtitle = ""
# Order that this section will appear in. # Number of recent posts to list.
weight = 40
# 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 count = 5
# List format. # List format.
@@ -25,5 +17,10 @@ count = 5
# 1 = Detailed # 1 = Detailed
# 2 = Stream # 2 = Stream
list_format = 2 list_format = 2
# Filter posts by tag or category.
# E.g. to only show posts tagged with `Academic`, set `filter_tag = "Academic"`
filter_tag = ""
filter_category = ""
+++ +++
+24 -70
View File
@@ -1,6 +1,19 @@
{{ $ := .root }} {{ $ := .root }}
{{ $page := .page }} {{ $page := .page }}
{{ $posts_len := len (where (where $.Site.RegularPages "Type" "post") ".Params.notonhomepage" nil) }}
{{ $query := where (where $.Site.RegularPages "Type" "post") ".Params.notonhomepage" nil }}
{{ $archive_page := $.Site.GetPage "section" "post" }}
{{/* Check if a filter should be applied. */}}
{{ if $page.Params.filter_tag }}
{{ $archive_page = $.Site.GetPage (printf "tags/%s" $page.Params.filter_tag) }}
{{ $query = $query | intersect $archive_page.Pages }}
{{ else if $page.Params.filter_category }}
{{ $archive_page = $.Site.GetPage (printf "categories/%s" $page.Params.filter_category) }}
{{ $query = $query | intersect $archive_page.Pages }}
{{ end }}
{{ $count := len ($query) }}
<!-- Blog Posts widget --> <!-- Blog Posts widget -->
<div class="row"> <div class="row">
@@ -8,9 +21,9 @@
<h1>{{ with $page.Title }}{{ . | markdownify }}{{ end }}</h1> <h1>{{ with $page.Title }}{{ . | markdownify }}{{ end }}</h1>
{{ with $page.Params.subtitle }}<p>{{ . | markdownify }}</p>{{ end }} {{ with $page.Params.subtitle }}<p>{{ . | markdownify }}</p>{{ end }}
{{ if gt $posts_len $page.Params.count }} {{ if gt $count $page.Params.count }}
<p class="view-all"> <p class="view-all">
<a href="{{ ($.Site.GetPage "section" "post").RelPermalink }}"> <a href="{{ $archive_page.RelPermalink }}">
{{ i18n "more_posts" | markdownify }} {{ i18n "more_posts" | markdownify }}
<i class="fas fa-angle-double-right"></i> <i class="fas fa-angle-double-right"></i>
</a> </a>
@@ -22,74 +35,15 @@
{{ with $page.Content }}<p>{{ . | markdownify }}</p>{{ end }} {{ with $page.Content }}<p>{{ . | markdownify }}</p>{{ end }}
{{ if gt (len $page.Params.tags_include) 0 }} {{ range $post := first $page.Params.count $query }}
{{ $posts := where (where (where $.Site.RegularPages "Type" "post") ".Params.tags" "intersect" $page.Params.tags_include) ".Params.notonhomepage" nil }} {{ if eq $page.Params.list_format 0 }}
{{ partial "post_li_simple" . }}
{{ $.Scratch.Add "show_post" "1" }} {{ else if eq $page.Params.list_format 1 }}
{{ range $post := first $page.Params.count $posts }} {{ partial "post_li_detailed" . }}
{{ else }}
{{ $.Scratch.Set "show_post" "1" }} {{ partial "post_li_stream" . }}
{{/* If `tags_include` is set, exclude posts with no tags. */}}
{{ with (not .Params.tags) }}
{{ with $page.Params.tags_include }}
{{ $.Scratch.Set "show_post" "0" }}
{{end}}
{{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" }}
{{ if eq $page.Params.list_format 0 }}
{{ partial "post_li_simple" . }}
{{ else if eq $page.Params.list_format 1 }}
{{ partial "post_li_detailed" . }}
{{ else }}
{{ partial "post_li_stream" . }}
{{ end }}
{{end}}
{{ end }} {{ end }}
{{ else}} {{end}}
{{ $posts := where (where $.Site.RegularPages "Type" "post") ".Params.notonhomepage" nil }}
{{ $.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. */}}
{{ with (not .Params.tags) }}
{{ with $page.Params.tags_include }}
{{ $.Scratch.Set "show_post" "0" }}
{{end}}
{{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" }}
{{ if eq $page.Params.list_format 0 }}
{{ partial "post_li_simple" . }}
{{ else if eq $page.Params.list_format 1 }}
{{ partial "post_li_detailed" . }}
{{ else }}
{{ partial "post_li_stream" . }}
{{ end }}
{{end}}
{{ end }}
{{ end }}
</div> </div>
</div> </div>