Skip to content

Commit 8917c68

Browse files
committed
Callout: Add named parms and better default handling
1 parent 78599a2 commit 8917c68

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
---
3+
description: Callout - Param types
4+
title: Callout - Param types
5+
weight: 100
6+
---
7+
8+
Hugo supports named an unnamed params.
9+
Named are more readable.
10+
The callout shortcode aims to support both, but **not** in the same shortcode instance.
11+
12+
13+
## The two callouts below using **unnamed** params
14+
15+
{{<call-out "" "Custom title" "fa fa-check-circle" "true">}}
16+
This callout uses the icon check-circle. **This should be an inline callout.**
17+
{{</call-out>}}
18+
19+
{{<call-out "" "Custom title" "fa fa-check-circle" "false">}}
20+
This callout uses the icon check-circle. **This should be an sideline callout.**
21+
{{</call-out>}}
22+
23+
## The two callouts below using **named** params
24+
This should work exactly the same as the two callouts above
25+
26+
{{<call-out title="Custom title" icon="fa fa-check-circle" inline="true">}}
27+
This callout uses the icon check-circle. **This should be an inline callout.**
28+
{{</call-out>}}
29+
30+
{{<call-out title="Custom title" icon="fa fa-check-circle" inline="asdas">}}
31+
This callout uses the icon check-circle. **This should be an sideline callout.**
32+
{{</call-out>}}

layouts/shortcodes/call-out.html

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
1-
{{ $class := .Get 0 }}
1+
{{ $class := .Get 0 | default (.Get "class") | default "" }}
2+
{{ $title := .Get 1 | default (.Get "title") | default "" }}
3+
{{ $icon := .Get 2 | default (.Get "icon") | default "" }}
4+
5+
{{/* Handle different versions of booleans */}}
6+
{{ $inlineParam := (.Get 3) | default (.Get "inline") | default "false" | lower }}
7+
{{- /* Validate the parameter strictly */ -}}
8+
{{ if not (in (slice "true" "false") $inlineParam) }}
9+
{{ warnf "The '<call-out>' Shortcode parameter 'inline' must be 'true' or 'false', but got: '%s'. This will now default to 'false'" $inlineParam}}
10+
{{ end }}
11+
12+
{{ $inline := eq $inlineParam "true" }}
13+
214
{{ $sideOption := "side-callout" }}
315
{{ $inlineOption := "inline-callout" }}
416

5-
<!-- Add default option for callouts that are "sideline" if one is not provided -->
6-
{{ if and (not (strings.Contains $class $sideOption)) (not (strings.Contains $class $inlineOption)) }}
7-
{{ $class = printf "%s %s" $class $sideOption }}
17+
{{ if $inline }}
18+
{{ $class = printf "%s %s" $class $inlineOption }}
19+
{{ else }}
20+
{{ $class = printf "%s %s" $class $sideOption }}
821
{{ end }}
922

1023
<!-- Blockquote element with a class that is the first parameter passed to the shortcode -->
1124
<blockquote class="{{ $class }}">
1225
<div>
1326
<!-- Check if the third parameter (icon class) is provided -->
14-
{{ with .Get 2 }}
27+
{{ with $icon }}
1528
<!-- If the icon class is provided, render an <i> element with the given class -->
1629
<i class="{{ . }}"></i>
1730
{{ end }}
1831
<!-- Render the second parameter (title) as a strong element -->
19-
<strong>{{ .Get 1 }}</strong><br/>
32+
<strong>{{ $title }}</strong><br/>
2033
<!-- Render the inner content of the shortcode, converting it from Markdown to HTML -->
2134
{{ .Inner | markdownify }}
2235
</div>

0 commit comments

Comments
 (0)