diff options
author | Paul <paul@brainspark.nl> | 2020-02-29 01:51:54 +0300 |
---|---|---|
committer | Paul <paul@brainspark.nl> | 2020-02-29 02:05:16 +0300 |
commit | db979b35a4a99e3a56af7b0307036edf34921e60 (patch) | |
tree | d0ec84eff7e53c18fbb42d78d4307befbfec3fc2 | |
parent | e24419cfaf2be745ffa87626d4e8f29e99320a42 (diff) |
Rewrite breadcrumbs to use return values and support structured
-rw-r--r-- | layouts/partials/breadcrumbs.html | 24 | ||||
-rw-r--r-- | layouts/partials/breadcrumbs/list/footer.html | 2 | ||||
-rw-r--r-- | layouts/partials/breadcrumbs/list/header.html | 2 | ||||
-rw-r--r-- | layouts/partials/breadcrumbs/list/item.html | 20 | ||||
-rw-r--r-- | layouts/partials/breadcrumbs/recursive.html | 22 | ||||
-rw-r--r-- | layouts/partials/breadcrumbs/structured/footer.html | 3 | ||||
-rw-r--r-- | layouts/partials/breadcrumbs/structured/header.html | 7 | ||||
-rw-r--r-- | layouts/partials/breadcrumbs/structured/header.html.bak | 9 | ||||
-rw-r--r-- | layouts/partials/breadcrumbs/structured/item.html | 25 | ||||
-rw-r--r-- | layouts/partials/head.html | 4 |
10 files changed, 98 insertions, 20 deletions
diff --git a/layouts/partials/breadcrumbs.html b/layouts/partials/breadcrumbs.html index 4ea756f..4a8d4b8 100644 --- a/layouts/partials/breadcrumbs.html +++ b/layouts/partials/breadcrumbs.html @@ -3,40 +3,50 @@ {{- $header := print "breadcrumbs/" $output_type "/header.html" }} {{- $item := print "breadcrumbs/" $output_type "/item.html" }} {{- $footer := print "breadcrumbs/" $output_type "/footer.html" }} +{{- $full_data := "" }} {{- with $ctx }} {{- $path := split .URL "/" }} - {{ partial $header }} + {{- $data := partial $header . }} + {{- $full_data = printf "%s%s" $full_data $data }} {{- if eq .Kind "taxonomyTerm" }} - {{ partial $item (dict "caption" .Title "url" "" "level" 1 "image" "" "final" true) }} + {{- $data := partial $item (dict "caption" .Title "url" "" "level" 1 "image" "" "final" true) }} + {{- $full_data = printf "%s%s" $full_data $data }} {{- else if eq .Kind "taxonomy" }} {{/* Find the taxonomyTerm and render */}} {{- $taxonomyTerm := .Site.GetPage "taxonomyTerm" .Data.Plural }} - {{ partial $item (dict "caption" $taxonomyTerm.Title "url" $taxonomyTerm.URL "level" 1 "image" "" "final" false) }} + {{- $data := partial $item (dict "caption" $taxonomyTerm.Title "url" $taxonomyTerm.URL "level" 1 "image" "" "final" false) }} + {{- $full_data = printf "%s%s" $full_data $data }} {{/* Render the taxonomy item */}} - {{ partial $item (dict "caption" .Title "url" .URL "level" 2 "image" "" "final" true) }} + {{- $data := partial $item (dict "caption" .Title "url" .URL "level" 2 "image" "" "final" true) }} + {{- $full_data = printf "%s%s" $full_data $data }} {{- else if or ( eq .Kind "section" ) ( eq .Kind "page" ) }} {{/* Display all relevant section elements */}} - {{- partial "breadcrumbs/recursive.html" (dict "output_type" $output_type "sections" .Site.Home.Sections "level" 1 "path" $path "dir" .URL ) }} + {{- $data := partial "breadcrumbs/recursive.html" (dict "output_type" $output_type "sections" .Site.Home.Sections "level" 1 "path" $path "dir" .URL ) }} + {{- $full_data = printf "%s%s" $full_data $data }} {{- if eq .Kind "page" }} {{- $aux := split .URL "/" }} - {{ partial $item (dict "caption" .Title "url" .URL "level" (sub (len $aux) 2 ) "image" .Params.share_img "final" true ) }} + {{- $data := partial $item (dict "caption" .Title "url" .URL "level" (sub (len $aux) 2 ) "image" .Params.share_img "final" true ) }} + {{- $full_data = printf "%s%s" $full_data $data }} {{- end }} {{- end }} - {{ partial $footer }} + {{- $data := partial $footer . }} + {{- $full_data = printf "%s%s" $full_data $data }} {{- end }} + +{{- $full_data | safeHTML}} diff --git a/layouts/partials/breadcrumbs/list/footer.html b/layouts/partials/breadcrumbs/list/footer.html index 0672325..fe30da4 100644 --- a/layouts/partials/breadcrumbs/list/footer.html +++ b/layouts/partials/breadcrumbs/list/footer.html @@ -1,2 +1,4 @@ +{{ return ` </ol> </nav> +`}} diff --git a/layouts/partials/breadcrumbs/list/header.html b/layouts/partials/breadcrumbs/list/header.html index 241a289..05dfe3d 100644 --- a/layouts/partials/breadcrumbs/list/header.html +++ b/layouts/partials/breadcrumbs/list/header.html @@ -1,5 +1,7 @@ +{{ return ` <nav aria-label="breadcrumb"> <ol class="breadcrumb"> <li class="breadcrumb-item active" aria-current="page"> <a href="/"><i class="fa fa-home"></i></a> </li> +`}} diff --git a/layouts/partials/breadcrumbs/list/item.html b/layouts/partials/breadcrumbs/list/item.html index 4443419..a6a759f 100644 --- a/layouts/partials/breadcrumbs/list/item.html +++ b/layouts/partials/breadcrumbs/list/item.html @@ -3,8 +3,20 @@ {{- $image := .image }} {{- $level := .level }} {{- $final := .final }} - <li class="breadcrumb-item {{ if eq $final true }}active{{ end }}" {{ if eq $final true }}aria-current="page"{{ end }}> - {{- if eq $final false }}<a href="{{ .url }}">{{- end }} - <small>{{ .caption }}</small> - {{- if eq $final false }}</a>{{- end }} + +{{- $full_data := "" }} +{{- if eq $final true }} + {{- $full_data = printf ` + <li class="breadcrumb-item active" aria-current="page"> + <small>%s</small> </li> + ` $caption }} +{{- else }} + {{- $full_data = printf ` + <li class="breadcrumb-item"> + <a href="%s"><small>%s</small></a> + </li> + ` $url $caption }} +{{- end }} + +{{ return $full_data }} diff --git a/layouts/partials/breadcrumbs/recursive.html b/layouts/partials/breadcrumbs/recursive.html index 97e8442..0994e9b 100644 --- a/layouts/partials/breadcrumbs/recursive.html +++ b/layouts/partials/breadcrumbs/recursive.html @@ -4,17 +4,21 @@ {{- $dir := .dir }} {{- $output_type := .output_type }} {{- $item := print "breadcrumbs/" $output_type "/item.html" }} +{{- $full_data := "" }} {{- range $sections }} - {{- $section := index $path $level }} - {{- $aux := split .URL "/" }} + {{- $section := index $path $level }} + {{- $aux := split .URL "/" }} - {{- if eq $section (index $aux $level) }} - {{ partial $item (dict "caption" .Title "url" .URL "image" .Params.share_img "level" $level "final" (eq ( add $level 2 ) (len $path) ) ) }} - - {{- if gt (len .Sections) 0 }} - {{- partial "breadcrumbs_recursive.html" ( dict "output_type" $output_type "sections" .Sections "level" (add $level 1) "path" $path "dir" $dir ) }} + {{- if eq $section (index $aux $level) }} + {{ $data := partial $item (dict "caption" .Title "url" .URL "image" .Params.share_img "level" $level "final" (eq ( add $level 2 ) (len $path) ) ) }} + {{ $full_data = printf "%s%s" $full_data $data }} - {{- end }} - {{- end }} + {{- if gt (len .Sections) 0 }} + {{- $data := partial "breadcrumbs_recursive.html" ( dict "output_type" $output_type "sections" .Sections "level" (add $level 1) "path" $path "dir" $dir ) }} + {{ $full_data = printf "%s%s" $full_data $data }} + {{- end }} + {{- end }} {{- end }} + +{{ return $full_data }} diff --git a/layouts/partials/breadcrumbs/structured/footer.html b/layouts/partials/breadcrumbs/structured/footer.html new file mode 100644 index 0000000..37a3fe6 --- /dev/null +++ b/layouts/partials/breadcrumbs/structured/footer.html @@ -0,0 +1,3 @@ +{{ return ` ] +} +</script>` }} diff --git a/layouts/partials/breadcrumbs/structured/header.html b/layouts/partials/breadcrumbs/structured/header.html new file mode 100644 index 0000000..d91a254 --- /dev/null +++ b/layouts/partials/breadcrumbs/structured/header.html @@ -0,0 +1,7 @@ +{{ return ` +<script type="application/ld+json"> +{ + "@context": "http://schema.org", + "@type": "BreadcrumbList", + "itemListElement": [ +` }} diff --git a/layouts/partials/breadcrumbs/structured/header.html.bak b/layouts/partials/breadcrumbs/structured/header.html.bak new file mode 100644 index 0000000..4af95f3 --- /dev/null +++ b/layouts/partials/breadcrumbs/structured/header.html.bak @@ -0,0 +1,9 @@ +{{ $str := ` +<script type="application/ld+json"> +{ + "@context": "http://schema.org", + "@type": "BreadcrumbList", + "itemListElement": [ +` }} +{{ $str := "asdasda" }} +{{ return $str }} diff --git a/layouts/partials/breadcrumbs/structured/item.html b/layouts/partials/breadcrumbs/structured/item.html new file mode 100644 index 0000000..ae6a8b4 --- /dev/null +++ b/layouts/partials/breadcrumbs/structured/item.html @@ -0,0 +1,25 @@ +{{- $url := .url }} +{{- $caption := .caption }} +{{- $image := .image }} +{{- $image_str := "" }} +{{- with $image }} +{{- $image_str = . | absURL }} +{{- end }} +{{- $level := .level }} +{{- $final := .final }} +{{- $final_str := "" }} +{{- if eq $final false }} +{{- $final_str = "," }} +{{- end }} +{{- $full_data := printf ` + { + "@type": "ListItem", + "position": %d, + "item": { + "@id": "%s", + "name": "%s", + "image": "%s" + } + }%s +` $level ( $url | absURL ) $caption $image_str $final_str }} +{{- return $full_data }} diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 6933782..2353c21 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -26,6 +26,10 @@ <!-- SEO --> {{- partial "seo/main.html" . -}} +{{- $showBreadcrumbs := $.Params.showBreadcrumbs | default $.Site.Params.showBreadcrumbs | default true }} +{{ if eq $showBreadcrumbs true -}} + {{ partial "breadcrumbs.html" ( dict "ctx" . "output_type" "structured" ) }} +{{- end }} <!-- Social Media Tags --> {{- partial "social/main.html" . -}} |