Skip to content

Instantly share code, notes, and snippets.

@jeff-mccarthy
Created May 7, 2018 20:54
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 jeff-mccarthy/f65165b2a14bd5863431562558f7c365 to your computer and use it in GitHub Desktop.
Save jeff-mccarthy/f65165b2a14bd5863431562558f7c365 to your computer and use it in GitHub Desktop.
Bootstrap Tabs Macro
{#
* Import
* {% import 'components.njx' as component %}
#}
{#
* Tabs (using bootstrap)
* Build out a bootstrap navigation and a tab content area
* @param {array} tabs = ['tab1', 'tab2', etc] | builds out nav using array values as ids and the text for that nav item
* @param {int} active = 1 | index of tab nav item that will be active
* @param {array} (optional) classes = ['hidden-xs', 'hidden-sm' ] | adds additional classes to the tab nav container
*
* Usage
* {% call component.tabs(['Tab 1', 'Tab 2'], active = 1, classes=['hidden-xs']) %}
* <!-- tabpane macro used here -->
* {% endcall %}
*
* Outputs
* <ul class="nav nav-tabs hidden-xs">
* <li class="active"><a href="#tab-1" data-toggle="tab">Tab 1</a></li>
* <li><a href="#tab-2" data-toggle="tab">Tab 2</a></li>
* </ul>
* <div class="tab-content"> <!-- tab panes go here --> </div>
#}
{% macro tabs(tabs = ['tab 1'], active = 1, classes=[]) %}
<ul class="nav nav-tabs {{ classes | join(' ') }}">
{% for tab in tabs %}
{% set id = tab | lower | replace(' ', '-') %}
{% if loop.index === active %}
<li class="active"><a href="#{{id}}" data-toggle="tab">{{tab}}</a></li>
{% else %}
<li><a href="#{{id}}" data-toggle="tab">{{tab}}</a></li>
{% endif %}
{% endfor %}
</ul>
<div class="tab-content">
{{ caller() }}
</div>
{% endmacro %}
{#
* Tab Pane (using bootstrap)
* A helper for the tab macro that builds a tab pane with custom content of your choice
* @param {string} id = 'home' | id of the tab pane - must match id of the associated tab nav item
* automatically converts to lowercase, and replaces spaces with dashes
* @param {boolean} active = false | make tab pane active
* @param {array} classes | adds addional classes to the tab pane
*
* Usage
* {% call component.tabpane(id = 'Contact Us', active = true, classes = ['something']) %}
* <!-- content here -->
* {% endcall %}
*
* Outputs
* <div class="tab-pane active something" id="contact-us"> ... </div>
#}
{% macro tabpane(id, active = false, classes = []) %}
{% if active %}
<div class="tab-pane active {{ classes | join(' ') }}" id="{{ id | lower | replace(' ', '-') }}">{{ caller() }}</div>
{% else %}
<div class="tab-pane {{ classes | join(' ') }}" id="{{ id | lower | replace(' ', '-') }}">{{ caller() }}</div>
{% endif %}
{% endmacro %}
@jeff-mccarthy
Copy link
Author

Experimental and didn't really pan out; only worth using if you have extremely simple tabs.

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