A Craft CMS Twig Extension for easier and human readable loop switcher.
Loop switcher, huh? Let's say in your template you want to display content inside a loop only for every 3rd loop, then on every 9th item, oh and every first and odd item too! Following code will be a way right? :
{% foriin1..10 %}
{% ifloop.indexisdivisibleby (3) %}
Content here display on every 3rd loop
{% endif %}
{% ifloop.indexisdivisibleby (9) %}
Content here display on every 9th loop
{% endif %}
{% ifloop.index==1 %}
Content here display on first loop
{% endif %}
{% ifloop.indexisodd %}
And content here display on every odd loop
{% endif %}
{% endfor %}With Switchcraft you can just do this :
{% foriin1..10 %}
{% switchcraftloop.index %}
{% on'every3Items' %}
Content here display on every 3rd loop
{% on'every9Items' %}
Content here display on every 9th loop
{% on'firstItem' %}
Content here display on first loop
{% on'oddItem' %}
And content here display on every odd loop
{% endswitchcraft %}
{% endfor %}And of course there are more options!
Download ZIP and unzip file then place the
switchcraftdirectory into yourcraft/pluginsdirectory.Install the plugin through Control Panel under
Settings > Plugins
Switchcraft comes with two methods : tag and filter, so you can choose them for your convenience.
You can use following tag format {% switchcraft %} … {% endswitchcraft %}, there are two ways to use this tag :
Format : {% switchcraft loop.index on 'key' %} … {% endswitchcraft %}
{% foriin1..10 %}
{% switchcraftloop.indexon'firstItem' %}
<p>This will be displayed on first loop</p>
{% endswitchcraft %}
{% switchcraftloop.indexon'oddItem' %}
<p>This will be displayed on odd loop</p>
{% endswitchcraft %}
{% switchcraftloop.indexon'every3Items' %}
<p>This will only be displayed on every 3rd loop</p>
{% endswitchcraft %}
...
{% endfor %}Format : {% switchcraft loop.index %} {% on 'key' %} ... {% endswitchcraft %}
{% foriin1..10 %}
{% switchcraftloop.index %}
{% on'lastItem' %}
<p>This will only be displayed on last loop</p>
{% on'every9Items' %}
<p>This will only be displayed on every 9th loop</p>
{% on'every5Items' %}
<p>This will only be displayed on every 5th loop</p>
...
{% endswitchcraft %}
{% endfor %}As with tag, Switchcraft's filter name is like following : switchcraft. There are three types of paramaters you can pass :
- Array :
switchcraft({ key : 'value', ... }) - Key + Value
switchcraft( 'key', 'value' ) - Key Only :
switchcraft( 'key' )
Example usage :
{% foriin1..10 %}
{% setclassName=loop.index | switchcraft({
firstItem : 'sample-first-class',
oddItem : 'sample-odd-class',
evenItem : 'sample-even-class',
every2Items : 'sample-2nd-class',
}) %}
<pclass="{{ className }}"> Content here... </p>
{% endfor %}Or if you prefer to pass directly instead store it to variable :
{% foriin1..10 %}
<pclass="{{ loop.index | switchcraft({ firstItem : 'sample-first-class', oddItem : 'sample-odd-class' }) }}">
Content here...
</p>
{% endfor %}Example usage :
{% foriin1..10 %}
<pclass="{{ loop.index | switchcraft( 'every2Items', 'sample-2nd-class') }}">
Content here...
</p>
{% endfor %}Especially for this type, Switchcraft will return a boolean value. Example usage :
This will check if loop is in every 3rd loop
{% foriin1..10 %}
{% ifloop.index | switchcraft( 'every3Items' ) %}
<p> I will only be displayed on every 3rd loop </p>
{% endif %}
{% endfor %}loop.index is a required parameter to read where you are. We're working on two additional support though : loop.index0 and context aware inside loop.
Following are available keys you can use :
firstItem: every first looplastItem: every last loopoddItem: every odd loopevenItem: every even loopeveryNItems: Where N is an integer number.
Switchcraft 2016 by Hidayat Sagita for Paper Tiger Marketing, LLC.