Help:Shortcode Tabs

Display content in tabs.

Tabs
Shortcode
Description
TypeHTML Shortcode
Nested?true
Shortcodetabs and tab
Return TypeHTML
Short Description
Use the tabs shortcode, together with the tab shortcode, to create clickable tabs to switch out content.
Development
MaintainerMichael Sasser

tabs is a nested shortcode which produces a tabbed view. To use tabs, you accordion. To use tabs, you need two shortcodes tabs and tab.

Parameters

The GitLab Label shortcode has the following parameters:

Tabs

ParameterDescription
nameA namespace to let Hugo distinguish them from each other

Tab

ParameterDescription
nameA name/label of the tab
codelang(optional) Render code blocks with syntax highlighting

Examples

{{< tabs name="some-name" >}}

{{< tab name="Python" codelang="python" >}}
print("Hello World")
{{< /tab >}}

{{< tab name="C" codelang="c" >}}
printf("Hello World");
{{< /tab >}}

{{< tab name="Description" >}}
This is just a description. No code gets rendered.
{{< /tab >}}}

{{< /tabs >}}
Rendered


print("Hello World")


printf("Hello World")

This is just a description. No code gets rendered.

Code

Below you find the implementation of the shortcode.

HTML

Tabs

Defined in layouts/shortcodes/tabs.html.

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

  Tabs Shortcode
  ==============

  Contributors: Michael Sasser
  Maintainer: Michael Sasser
  License: MIT
  Version: 1
  Child Shortcodes:
    - Tab Shortcode
  Parent Shortcodes: None

  LAYOUT: /layouts/shortcodes/tabs.html
  STYLE:  /assets/scss/components/_shortcode_tabs.scss
  DOCS:   /content/<language>/wiki/Help:Shortcode_Tabs.md

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

  Use the tab shortcode together with the tabs shortcode to display tabs.

  Changelog
  =========

  Version 1
  ---------

  Initial release

-->

{{/* prettier-ignore-start */}}
{{- .Page.Scratch.Add "tabset-counter" 1 -}}
{{- $tab_set_id := .Get "name" | default (printf "tabset-%s-%d" (.Page.RelPermalink) (.Page.Scratch.Get "tabset-counter") ) | anchorize -}}
{{- $tabs := .Scratch.Get "tabs" -}}
{{- if .Inner -}}{{- /* We don't use the inner content, but Hugo will complain if we don't reference it. */ -}}{{- end -}}
<ul class="nav nav-tabs nav-fill mb-4" id="{{ $tab_set_id }}" role="tablist">
        {{- range $i, $e := $tabs -}}
          {{- $id := printf "%s-%d" $tab_set_id $i -}}
          {{- if (eq $i 0) -}}
                <li class="nav-item">
                        <button data-bs-toggle="tab" class="nav-link active" href="#{{ $id }}" type="button" role="tab" aria-controls="{{ $id }}" aria-selected="true">{{- trim .name " " -}}</button>
                </li>
          {{ else }}
                <li class="nav-item">
                        <button data-bs-toggle="tab" class="nav-link" href="#{{ $id }}" type="button" role="tab" aria-controls="{{ $id }}" aria-selected="false">{{- trim .name " " -}}</button>
                </li>
          {{- end -}}
{{- end -}}
</ul>
<div class="tab-content" id="{{ $tab_set_id }}">
{{- range $i, $e := $tabs -}}
{{- $id := printf "%s-%d" $tab_set_id $i -}}
{{- if (eq $i 0) -}}
  <div id="{{ $id }}" class="tab-pane show active" role="tabpanel" aria-labelledby="{{ $id }}">
{{ else }}
  <div id="{{ $id }}" class="tab-pane" role="tabpanel" aria-labelledby="{{ $id }}">
{{ end }}
<p>
        {{- with .content -}}
                {{- . -}}
        {{- else -}}
                {{- if eq $.Page.BundleType "leaf" -}}
                        {{- /* find the file somewhere inside the bundle. Note the use of double asterisk */ -}}
                        {{- with $.Page.Resources.GetMatch (printf "**%s*" .include) -}}
                                {{- if ne .ResourceType "page" -}}
                                {{- /* Assume it is a file that needs code highlighting. */ -}}
                                {{- $codelang := $e.codelang | default ( path.Ext .Name | strings.TrimPrefix ".") -}}
                                {{- highlight .Content $codelang "" -}}
                                {{- else -}}
                                        {{- .Content -}}
                                {{- end -}}
                        {{- end -}}
                {{- else -}}
                {{- $path := path.Join $.Page.File.Dir .include -}}
                {{- $page := site.GetPage "page" $path -}}
                {{- with $page -}}
                        {{- .Content -}}
                {{- else -}}
                {{- errorf "[%s] tabs include not found for path %q" site.Language.Lang $path -}}
                {{- end -}}
                {{- end -}}
        {{- end -}}
</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.

  • Tabs Shortcode
    tabs.html3.39 KB
  • md5   0122b75d385805e762acd022a32bc456
  • sha1   85328341881710084cb4db624fdac4caa1af6982
  • sha256   4975d0e4ae9dda55fd1e9d9afd40658c5a5471bb999707a9c2af6a0d3a53e8b7

Tab

Defined in layouts/shortcodes/tab.html.

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

  Tab Shortcode
  =============

  Contributors: Michael Sasser
  Maintainer: Michael Sasser
  License: MIT
  Version: 1
  Child Shortcodes: None
  Parent Shortcodes:
    - Tab Shortcode

  LAYOUT: /layouts/shortcodes/tab.html
  STYLE:  /assets/scss/components/_shortcode_tab.scss
  DOCS:   /content/<language>/wiki/Help:Shortcode_Tabs.md

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

  Use the tab shortcode together with the tabs shortcode to display tabs.

  Changelog
  =========

  Version 1
  ---------

  Initial release

-->

{{/* prettier-ignore-start */}}
{{ if .Parent }}
	{{ $name := trim (.Get "name") " " }}
	{{ $include := trim (.Get "include") " "}}
	{{ $codelang := .Get "codelang" }}
	{{ if not (.Parent.Scratch.Get "tabs") }}
  	{{ .Parent.Scratch.Set "tabs" slice }}
	{{ end }}
	{{ with .Inner }}
  	{{ if $codelang }}
      {{ $.Parent.Scratch.Add "tabs" (dict "name" $name "content" (highlight . $codelang "") ) }}
    {{ else }}
      {{ $.Parent.Scratch.Add "tabs" (dict "name" $name "content" . ) }}
    {{ end }}
  {{ else }}
    {{ $.Parent.Scratch.Add "tabs" (dict "name" $name "include" $include "codelang" $codelang) }}
	{{ 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.

  • Tab Shortcode
    tab.html1.29 KB
  • md5   94c1aba15c69503e8dbe4f2df9d6ff80
  • sha1   a3b1290c15316de05aae6378c907cd3d13f53e9d
  • sha256   585c8b83aaed9e07dd8902b98e4a58d6ebaa2e101a7d6fafa95ac9d662dfeca5

SASS

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

This code is licensed under the MIT license.
/*

  Tabs Shortcode
  ==============

  Contributors: Michael Sasser
  Maintainer: Michael Sasser
  License: MIT
  Version: 1
  Child Shortcodes:
    - Tab Shortcode
  Parent Shortcodes: None

  LAYOUT: /layouts/shortcodes/tabs.html
  STYLE:  /assets/scss/components/_shortcode_tabs.scss
  DOCS:   /content/<language>/wiki/Help:Shortcode_Tabs.md

  Tab Shortcode
  =============

  LAYOUT: /layouts/shortcodes/tab.html
  STYLE:  /assets/scss/components/_shortcode_tab.scss
  DOCS:   /content/<language>/wiki/Help:Shortcode_Tabs.md

*/

/* Adapting nav-tabs for dark mode */
@include color-mode(dark) {
  button.nav-link.active {
    background-color: $body-bg-dark;
    color: $body-color-dark;
    border-color: $border-dark;
  }

  button.nav-link {
    color: $link-color-dark;
  }

  .nav-tabs {
    border-bottom: 1px solid $border-dark;
  }
}

Download

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

  • Tabs Style
    _shortcode_tabs.scss850.00 Bytes
  • md5   64378660903438b1b7771ff9587f9df1
  • sha1   dec61651ec74c3d496c367ec16adc36b33d797fd
  • sha256   e09c0db683cdd588839bd892d803cede7ffce0a062347299ced83eeec977b32b
Top