Skip to content

Commit 450466d

Browse files
committed
fix: related-content: Handle missing file context
This commit addresses an issue where `$page_context.File` could be nil, causing errors. It adds checks to ensure file context exists before accessing its properties. Also the software tag comparison should not happen on pages where the slug is empty. The thumbnail rendering was adjusted to only be rendered when an image is specified in the front matter.
1 parent daf7f64 commit 450466d

File tree

1 file changed

+26
-38
lines changed

1 file changed

+26
-38
lines changed

layouts/partials/shared/related-content.html

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
{{ $related_items := slice }}
33

44
{{ $current_type := $page_context.Type }}
5-
{{ $current_slug := path.Base $page_context.File.Dir }}
5+
{{ $current_slug := "" }}
6+
{{ if $page_context.File }}
7+
{{ $current_slug = path.Base $page_context.File.Dir }}
8+
{{ end }}
69
{{ $current_title := $page_context.Title }}
710

811

@@ -15,7 +18,7 @@
1518
{{ $tag_param_key = ".Params.software_tags" }}
1619
{{ end }}
1720

18-
{{ if ne $tag_param_key "" }}
21+
{{ if and (ne $tag_param_key "") (ne $current_slug "") }}
1922
{{ $tagged_pages := where site.RegularPages $tag_param_key "intersect" (slice $current_slug) }}
2023
{{ range $tagged_pages }}
2124
{{ $related_items = $related_items | append . }}
@@ -31,14 +34,15 @@
3134
{{ $software_slugs_to_find := . }}
3235
{{ $all_software_pages := where site.RegularPages "Type" "neuromorphic-software" }}
3336
{{ range $software_page := $all_software_pages }}
34-
{{ $software_slug := path.Base $software_page.File.Dir }}
35-
{{ if in $software_slugs_to_find $software_slug }}
37+
{{ $software_slug := "" }}
38+
{{ if $software_page.File }}{{ $software_slug = path.Base $software_page.File.Dir }}{{ end }}
39+
{{ if and (ne $software_slug "") (in $software_slugs_to_find $software_slug) }}
3640
{{ $related_items = $related_items | append $software_page }}
3741
{{ end }}
3842
{{ end }}
3943
{{ end }}
4044

41-
{{ else if eq $current_type "neuromorphic-software" }}
45+
{{ else if and (eq $current_type "neuromorphic-software") (ne $current_slug "") }}
4246
{{/* I am a software page. Find hardware that supports me. */}}
4347
{{ $hardware_pages := where site.RegularPages "Type" "neuromorphic-hardware" }}
4448
{{ range where $hardware_pages ".Params.software_tags" "intersect" (slice $current_slug) }}
@@ -47,7 +51,7 @@
4751
{{ end }}
4852

4953
{{/* --- Finalize and Render --- */}}
50-
{{/* Deduplicate without uniqBy for older Hugo versions */}}
54+
{{/* Deduplicate */}}
5155
{{ $unique_related_items := slice }}
5256
{{ $seen_permalinks := slice }}
5357
{{ range $related_items }}
@@ -68,47 +72,31 @@ <h2 class="text-3xl font-bold mb-8">Related Content, Events & Resources</h2>
6872
<div class="timeline-item-content">
6973
<div class="flex flex-col md:flex-row items-start gap-5">
7074

71-
{{/* Thumbnail Column */}}
72-
<div class="flex-shrink-0 w-full md:w-40 text-center">
73-
<a href="{{ $item.RelPermalink }}">
74-
{{ $image_param := $item.Params.image }}
75-
{{ $img_path := "" }}
76-
{{ $img_alt := $item.Title }}
77-
78-
{{ if $image_param }}
75+
{{/* Thumbnail Column - Renders only if image is in front matter */}}
76+
{{ with $item.Params.image }}
77+
<div class="flex-shrink-0 w-full md:w-40 text-center">
78+
<a href="{{ $item.RelPermalink }}">
79+
{{ $image_param := . }}
80+
{{ $img_path := "" }}
81+
{{ $img_alt := $item.Title }}
82+
7983
{{ $img_path_cleaned := strings.TrimPrefix "/" $image_param }}
8084
{{ $resource := "" }}
81-
{{ with $item.Resources.GetMatch $image_param }}
82-
{{ $resource = . }}
83-
{{ else }}
84-
{{ with resources.Get $img_path_cleaned }}
85-
{{ $resource = . }}
86-
{{ end }}
87-
{{ end }}
88-
85+
{{ with $item.Resources.GetMatch $image_param }}{{ $resource = . }}{{ end }}
86+
{{ if not $resource }}{{ with resources.Get $img_path_cleaned }}{{ $resource = . }}{{ end }}{{ end }}
87+
8988
{{ if $resource }}
9089
{{ $img_path = ($resource.Fill "160x90 Lanczos").RelPermalink }}
9190
{{ else if fileExists (printf "static/%s" $img_path_cleaned) }}
9291
{{ $img_path = $image_param | relURL }}
9392
{{ end }}
94-
{{ end }}
9593

96-
{{ if not $img_path }}
97-
{{ with resources.Get "images/workshop-thumbnail-default.png" }}
98-
{{ $img_path = (.Fill "160x90 Lanczos").RelPermalink }}
99-
{{ $img_alt = "Default Open Neuromorphic Event Thumbnail" }}
94+
{{ if $img_path }}
95+
<img src="{{ $img_path }}" alt="{{ $img_alt }}" class="w-full h-auto rounded-md object-cover">
10096
{{ end }}
101-
{{ end }}
102-
103-
{{ if $img_path }}
104-
<img src="{{ $img_path }}" alt="{{ $img_alt }}" class="w-full h-auto rounded-md object-cover">
105-
{{ else }}
106-
<div class="w-full h-[90px] bg-gray-200 dark:bg-darkmode-theme-dark rounded-md flex items-center justify-center">
107-
{{ partial "icon.html" (dict "style" "regular" "name" "images" "class" "text-4xl text-gray-400") }}
108-
</div>
109-
{{ end }}
110-
</a>
111-
</div>
97+
</a>
98+
</div>
99+
{{ end }}
112100

113101
{{/* Content Column */}}
114102
<div class="flex-grow">

0 commit comments

Comments
 (0)