Twig Perversion plugin for Craft CMS
Obituary
Twig Perversions tags no longer work as of twig 3.12 (Craft version 5.4). Twig used to compile its macros into fairly straightforward functions, which Twig Perversion was able to hack. They are now using generator functions, and the old hacks no longer work., (*1)
Intro
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., (*2)
-
{% while %}
, {% break %}
, {% continue %}
, and {% return %}
tags
-
===
, !==
, and <=>
operators
-
is numeric
, is string
, and is array
tests
-
array_splice
, string
, float
, int
, and bool
filters
Requirements
This plugin requires Craft CMS 3.1.29 or later., (*3)
Installation
- Install with Composer via
composer require marionnewlevant/twig-perversion
from your project directory
- Install plugin in the Craft Control Panel under Settings > Plugins
or, (*4)
- Install via the Plugin Store
Using Twig Perversion
-
{% while %}
loop:, (*5)
{% set x = 0 %}
{% while x < 5 %}
{# do whatever... #}
{% set x = x + 1 %}
{% endwhile %}
Note that twig's loop variables (except for revindex
, revindex0
, last
, and length
) are available inside of a while loop, as are the {% break %}
and {% continue %}
tags., (*6)
-
{% break %}
to exit a for loop or while loop:, (*7)
{% for straw in haystack %}
{% if straw == needle %}
{% break %}
{% endif %}
{% endfor %}
{% while true %}
{# do whatever... #}
{% if someCondition %}
{% break %}
{% endif %}
{% endwhile %}
-
{% continue %}
to continue to next iteration:, (*8)
{% for straw in haystack %}
{% if not isInteresting(straw) %}
{% continue %}
{% endif %}
{# do whatever... #}
{% endfor %}
-
{% return value %}
to return a value from a macro:, (*9)
{% macro foo() %}
{# ... calculate someValue ... #}
{% return someValue %}
{% endmacro %}
-
{% return %}
to return the empty string from a macro:, (*10)
{% 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., (*11)
Operators
-
=== and !==, (*12)
Compares two values for equivalence, using php's ===
and !==
operators., (*13)
-
<=>, (*14)
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., (*15)
Tests
{{ 12 is numeric ? 'Yes' : 'No' }}
{# Yes #}
{{ '-1.3' is numeric ? 'Yes' : 'No' }}
{# Yes #}
{{ '0x539' is numeric ? 'Yes' : 'No'}}
{# No #}
{{ '12' is string ? 'Yes' : 'No' }}
{# Yes #}
{{ 12 is string ? 'Yes' : 'No' }}
{# No #}
{ [] is array ? 'Yes' : 'No' }}
{# Yes #}
{{ '12' is array ? 'Yes' : 'No' }}
{# No #}
Filters
-
array_splice, (*22)
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
., (*23)
-
string, (*24)
Typecast variable as a string., (*25)
-
float, (*26)
Typecast variable as a float., (*27)
-
int, (*28)
Typecast variable as an integer., (*29)
-
bool, (*30)
Typecast variable as a boolean., (*31)
Brought to you by Marion Newlevant, (*32)