I’m trying to add configurable sidebar widgets to my theme, where users can declare new widgets using partials and some configurations under Params
, then specify what order they want those widgets to appear in. The theme comes with its own widgets, which are defined in the theme’s configuration file.
My code tries to access the widget’s configuration from the site’s Params
and fall back to the theme’s configuration. Thing is, trying to directly access the theme’s configuration through $.Site.Params.bluestnight
(theme name is BluestNight) returns nil. Am I doing something wrong, or is this a bug in Hugo?
As a side note, I think Hugo could/should implement a more granular combining of Params
: I would expect the widget definitions in the theme config to be available through $.Site.Params.sidebar.*
even though the site has defined $.Site.Params.sidebar.order
. This issue came about when I tried to work around it.
Theme configuration:
[params.sidebar]
[params.sidebar.patreon]
path = "widgets/patreon.html"
cache = true
[params.sidebar.search]
path = "widgets/search.html"
cache = true
[params.sidebar.recent]
path = "widgets/recent.html"
[params.sidebar.events]
path = "widgets/events.html"
cache = true
[params.sidebar.categories]
path = "widgets/categories.html"
cache = true
[params.sidebar.taglist]
path = "widgets/taglist.html"
cache = true
Site configuration:
[params.sidebar]
order = [
"search",
"patreon",
"categories",
"recents"
]
Code:
{{/* $.Scratch.Get "sidebar" set to a comma-separated string in above code */}}
{{/* based on $.Site.Params.sidebar.order or previous method of enabling widgets */}}
{{- $widgets := (split (strings.TrimRight "," ($.Scratch.Get "sidebar")) ",") -}}
{{- $sidebar := $.Site.Params.sidebar -}}
{{- $bnsidebar := $.Site.Params.bluestnight.sidebar -}}
{{- $scope := . -}}
{{- range $name := $widgets -}}
{{- $sb := cond (or (eq nil $sidebar) (eq nil (index $sidebar $name))) $bnsidebar $sidebar -}}
{{- if eq nil $sb -}}
{{- errorf "Sidebar widget definitions are nil: %#v" $sb -}}
{{- else if eq nil (index $sb $name) -}}
{{- errorf "Could not find definition for widget: %s" $name -}}
{{- end -}}
{{- $widget := index $sb $name -}}
{{- if $widget.cache -}}
{{- partialCached (string $widget.path) $scope -}}
{{- else -}}
{{- partial (string $widget.path) $scope -}}
{{- end -}}
{{- end -}}