Skip to content
This repository was archived by the owner on Oct 26, 2022. It is now read-only.

Repository files navigation

Switchcraft

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!

Installation

  1. Download ZIP and unzip file then place the switchcraft directory into your craft/plugins directory.

  2. Install the plugin through Control Panel under Settings > Plugins

Usage

Switchcraft comes with two methods : tag and filter, so you can choose them for your convenience.

Tag

You can use following tag format {% switchcraft %} … {% endswitchcraft %}, there are two ways to use this tag :

Tag with direct checking

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 %}

Tag per block checking

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 %}

Filter

As with tag, Switchcraft's filter name is like following : switchcraft. There are three types of paramaters you can pass :

  1. Array : switchcraft({ key : 'value', ... })
  2. Key + Valueswitchcraft( 'key', 'value' )
  3. Key Only : switchcraft( 'key' )

Array

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 %}

Key + Value

Example usage :

{% foriin1..10 %}
<pclass="{{ loop.index | switchcraft( 'every2Items', 'sample-2nd-class') }}">
Content here...
</p>
{% endfor %}

Key only

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

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.

Available Keys

Following are available keys you can use :

  • firstItem : every first loop

  • lastItem : every last loop

  • oddItem : every odd loop

  • evenItem : every even loop

  • everyNItems : Where N is an integer number.

License

Switchcraft 2016 by Hidayat Sagita for Paper Tiger Marketing, LLC.

About

A Craft CMS Twig Extension for easy, human readable loop switches.

Resources

Stars

10 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages