Help:Shortcode Accordion

Display content in vertically collapsing accordions.

Accordion
Shortcode
Description
TypeHTML Shortcode
Nested?true
Shortcodeaccordion and accordion_item
Return TypeHTML
Short Description
Use the accordion shortcode, together with the accordion_item shortcode, to create collapsible containers which hide information behind an headers.
Development
MaintainerMichael Sasser

accordion_item is a nested shortcode which produces vertically collapsing accordions elements in an accordion. To use accordion, you need two shortcodes accordion and accordion_item.

Parameters

The Accordion shortcodes have the following parameters:

accordion

The accordion is the outer wrapper for the accordion_item elements. It is the part, which actually renders the elements.

ParameterDescription
htmlif false (default), markdownify the content.

accordion_item

The accordion_item provides the outer wrapper accordion with all the data needed to render the individual elements. A sole use of accordion_item is not possible.

ParameterDescription
nameA name/label of the tab

Examples

{{< accordion >}}

{{< accordion_item name="A test" >}}
This is just a test.
{{< /accordion_item >}}

{{< accordion_item name="Another Test" >}}
Just another Test.
{{< /accordion_item >}}

{{< /accordion >}}
Rendered

This is just a test.

Just another Test.

Code

Below you find the implementation of the shortcode.

HTML

Accordion

Defined in layouts/shortcodes/accordion.html.

This code is licensed under the MIT license.
<!-- 

  Accordion Shortcode
  ===================

  Contributors: Michael Sasser
  Maintainer: Michael Sasser
  License: MIT
  Version: 1
  Child Shortcodes: 
    - accordion_item (mandatory)
  Parent Shortcodes: None

  LAYOUT: /layouts/shortcodes/accordion.html
  STYLE:  /assets/scss/components/_shortcode_accordion.scss
  DOCS:   /content/<language>/wiki/Help:Shortcode_Accordion.md

  Description
  -----------

  Use the accordion, together with the accordion_item, shortcode shortcode to
  create a collapsible accordion.

  Changelog
  =========

  Version 1
  ---------

  Initial release

-->


{{/* prettier-ignore-start */}}
{{ $is_html := .Get "html" | default false }}

{{- .Page.Scratch.Add "accordionset-counter" 1 -}}
{{- $accordion_set_id := .Get "name" | default (printf "accordionset-%s-%d" (.Page.RelPermalink) (.Page.Scratch.Get "accordionset-counter") ) | anchorize -}}
{{- $accordion_items := .Scratch.Get "accordion_item" -}}

{{- if .Inner -}}{{- /* Needed for the accordion to work, but not used  */ -}}{{- end -}}

<div class="accordion accordion-flush" id="accordionFlush-{{- $accordion_set_id -}}">
  {{- range $i, $e := $accordion_items -}}
    {{- $id := printf "%s-%d" $accordion_set_id $i -}}
    <div class="accordion-item">
      <h2 class="accordion-header" id="flush-heading-{{- $id -}}">
        <button
          class="accordion-button collapsed"
          type="button"
          data-bs-toggle="collapse"
          data-bs-target="#flush-collapse-{{- $id -}}"
          aria-expanded="false"
          aria-controls="flush-collapse-{{- $id -}}"
        >
          {{- trim .name " " -}}
        </button>
      </h2>
      <div
        id="flush-collapse-{{- $id -}}"
        class="accordion-collapse collapse"
        aria-labelledby="flush-heading-{{- $id -}}"
        data-bs-parent="#accordionFlush-{{- $accordion_set_id -}}"
      >
        <div class="accordion-body">
        {{- with .content -}}
          {{- if $is_html -}}
            {{- . -}}
          {{- else -}}
            {{- . | markdownify -}}
          {{- end -}}
        {{- end -}}
        </div>
      </div>
    </div>
  {{- end -}}
</div>
{{/* prettier-ignore-end */}}
Download

Copy the source code above or use the download link below to use this file on your website according to the license.

  • Accordion Shortcode
    accordion.html2.19 KB
  • md5   526a55970ac914bb4f82b258db7587f8
  • sha1   ddde886379d1e32e7b1457f2135a9744a3588a32
  • sha256   1ed698f5f3f840fd5d46b35ca8c4511c4b2e452f5a796713d59282dfc67995ef

Accordion Item

Defined in layouts/shortcodes/accordion_item.html.

This code is licensed under the MIT license.
<!-- 

  Accordion Item Shortcode
  ========================

  Contributors: Michael Sasser
  Maintainer: Michael Sasser
  License: MIT
  Version: 1
  Child Shortcodes: None
  Parent Shortcodes:
    - accordion (mandatory)

  LAYOUT: /layouts/shortcodes/accordion_item.html
  STYLE:  /assets/scss/components/_shortcode_accordion.scss
  DOCS:   /content/<language>/wiki/Help:Shortcode_Accordion.md

  Description
  -----------

  Use the accordion, together with the accordion_item, shortcode shortcode to
  create a collapsible accordion.

  Changelog
  =========

  Version 1
  ---------

  Initial release

-->

{{/* prettier-ignore-start */}}
{{ if .Parent }}
	{{ $name := trim (.Get "name") " " }}
	{{ $include := trim (.Get "include") " "}}
	{{ if not (.Parent.Scratch.Get "accordion_item") }}
  	{{ .Parent.Scratch.Set "accordion_item" slice }}
	{{ end }}
	{{ with .Inner }}
      {{ $.Parent.Scratch.Add "accordion_item" (dict "name" $name "content" . ) }}
  {{ else }}
    {{- errorf "[%s] %q: tab shortcode missing its inner content" site.Language.Lang .Page.Path -}}
	{{ end }}
{{ else }}
	{{- errorf "[%s] %q: tab shortcode missing its parent" site.Language.Lang .Page.Path -}}
{{ end}}
{{/* prettier-ignore-end */}}
Download

Copy the source code above or use the download link below to use this file on your website according to the license.

  • Accordion Item Shortcode
    accordion_item.html1.23 KB
  • md5   a66536a73f2c5a74b2425ed1e10f3634
  • sha1   d9eafa166d98bac7186af98f99449bb4cc17d234
  • sha256   72c1575095946f9936c1f6448723102a2d3b218c2b3c2d968b5c4dc74a4a0bfa

SASS

Defined in: /assets/scss/components/_shortcode_accordion.scss

This code is licensed under the MIT license.
/*

  Accordion Shortcode
  ===================

  Contributors: Michael Sasser
  Maintainer: Michael Sasser
  License: MIT
  Version: 1
  Child Shortcodes:
    - accordion_item (mandatory)
  Parent Shortcodes: None

  LAYOUT: /layouts/shortcodes/accordion.html
  STYLE:  /assets/scss/components/_shortcode_accordion.scss
  DOCS:   /content/<language>/wiki/Help:Shortcode_Accordion/index.md

*/

.accordion-header {
  margin-top: 0;
}

@include color-mode(dark) {
  .accordion {
    --#{$prefix}accordion-color: $body-bg-dark;
    --#{$prefix}accordion-border-color: #{$gray-800};
  }

  .accordion-button {
    color: $gray-300;
    background-color: $body-bg-dark;
  }

  .accordion-item {
    background-color: $border-dark;
  }

  .accordion-body {
    table {
      --bs-table-bg: $gray-300;
      --#{$prefix}accordion-bg: #{$gray-300};
    }

    thead,
    tbody,
    tfoot,
    tr,
    td,
    th {
      border-color: $gray-300;
    }
  }

  .accordion-button::after {
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%239adafb'%3e%3cpath fill-rule='evenodd' d='M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z'/%3e%3c/svg%3e");
  }

  .accordion-button:not(.collapsed) {
    color: #{$black};
    background-color: $focus-color-dark;
    box-shadow: inset 0 -1px 0 rgba(29, 45, 53, 0.125);
  }

  .accordion-button:not(.collapsed)::after {
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='%23212529'%3e%3cpath fill-rule='evenodd' d='M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z'/%3e%3c/svg%3e");

    /* transform: rotate(-180deg); */
  }
}

Download

Copy the source code above or use the download link below to use this file on your website according to the license.

  • Accordion Style
    _shortcode_accordion.scss1.83 KB
  • md5   c9ebd46ea09ac42b35eda6e45abbfd02
  • sha1   8aea1bde66c08f04d165d7a8d854f32a6718abfa
  • sha256   e0e0a12d4335b9ae9190b9871cf2036ad063b798dc5dff70fb27591466c322b1
Top