Making twig do things it really shouldn't. Break, continue, and return tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
CHANGELOG.md
LICENSE.txt
README.md
composer.json

README.md

Twig Perversion plugin for Craft CMS

Making twig do things it really shouldn't. Twig is not intended to be a general purpose programming language, and there are some things that really don't belong in the language. This plugin adds a few of those things anyway.

  • {% break %}, {% continue %}, and {% return %} tags
  • ===, !==, and <=> operators
  • is numeric test
  • json_decode filter
  • array_splice filter
  • string filter
  • float filter
  • int filter
  • bool filter

Installation

  1. Install with Composer via composer require marionnewlevant/twig-perversion from your project directory
  2. Install plugin in the Craft Control Panel under Settings > Plugins

or

  1. Install via the Plugin Store

Using Twig Perversion

Tags

{% break %} to exit a for loop:

{% for straw in haystack %}
  {% if straw == needle %}
    {% break %}
  {% endif %}
{% endfor %}

{% continue %} to continue to next iteration:

{% for straw in haystack %}
  {% if not isInteresting(straw) %}
    {% continue %}
  {% endif %}
  {# do whatever... #}
{% endfor %}

{% return value %} to return a value from a macro:

{% macro foo() %}
  {# ... calculate someValue ... #}
  {% return someValue %}
{% endmacro %}

{% return %} to return the empty string form a macro:

{% macro foo() %}
  {# ... do stuff %}
  {% return %}
{% endmacro %}

A macro with a {% return %} tag will return whatever the return value is (which can be a complex expression). Any other output generated by the macro will be discarded.

Operators

  • === and !==

    Compares two values for equivalence, using php's === and !== operators.

  • <=>

    Compares two values using php's <=> (spaceship) operator. Returns -1, 0, or 1 when the first argument is respectively less than, equal to, or greater than the second.

Tests

  • Numeric

    Test whether given value is numeric (behaviour like PHP 7 is_numeric). (Note that as of PHP 7, hexadecimal strings are not considered numeric)

Examples

{{ 12 is numeric ? 'Yes' : 'No' }}
{# Yes #}

{{ '-1.3' is numeric ? 'Yes' : 'No' }}
{# Yes #}

{{ '0x539' is numeric ? 'Yes' : 'No'}}
{# No #}

Filters

  • json_decode

    Decode json string, returning php associative arrays. Uses the PHP json_decode function

  • array_splice

    Remove a portion of an array and replace it with something else. Uses the PHP array_splice function. Note that unlike the php function, this filter returns the modified array rather than the extracted elements. The original array is unchanged. Since the implementation requires copying the array, this will be less efficient than the raw php function. The array_splice filter is passed an offset, an optional length, and an optional replacement.

  • string

    Typecast variable as a string.

  • float

    Typecast variable as a float.

  • int

    Typecast variable as an integer.

  • bool

    Typecast variable as a boolean.

Brought to you by Marion Newlevant