Skip to content

Instantly share code, notes, and snippets.

@GHSVS-de
Created March 14, 2022 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GHSVS-de/654330fe4f7a34037a93126911f5c7c8 to your computer and use it in GitHub Desktop.
Save GHSVS-de/654330fe4f7a34037a93126911f5c7c8 to your computer and use it in GitHub Desktop.

Product 1 with named param

Product 1 ({{< product type="prod1" >}}) is really great.

Product 2 with named param

{{< product type="prod2" >}}

Product 3 with named param

prod3 is unknown in shortcode product.html (see dict there).

{{< product type="prod3" >}}

Product 1

{{< product "prod1" >}}

Product 2

{{< product "prod2" >}}

{{- $product := "" -}}
{{- if .IsNamedParams -}}
{{- $product = .Get "type" -}}
{{- else -}}
{{- $product = .Get 0 -}}
{{- end -}}
{{- $products := dict
"prod1" "Orange"
"prod2" "Apple"
"NO_PRODUCT_MATCHES" "No product found for key \"%s\"!"
-}}
{{- $output := index $products $product -}}
{{- if not $output -}}
{{- $output = printf (index $products "NO_PRODUCT_MATCHES") $product -}}
{{- end -}}
{{- $output -}}
<h2>Product 1 with named param</h2>
<p>Product 1 (Orange) is really great.</p>
<h2>Product 2 with named param</h2>
Apple
<h2>Product 3 with named param</h2>
<p><code>prod3</code> is unknown in shortcode (see <code>dict</code> there).</p>
No product found for key "prod3"!
<h2>Product 1</h2>
Orange
<h2>Product 2</h2>
Apple
@jnnanav-aws
Copy link

awesome! thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment