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
is numeric
testjson_decode
filter
To install MN Twig Perversion, follow these steps:
- Download & unzip the file and place the
mntwigperversion
directory into yourcraft/plugins
directory - Install plugin in the Craft Control Panel under Settings > Plugins
MN Twig Perversion works on Craft 2.4.x, Craft 2.5.x, Craft 2.6.x, and probably earlier versions as well.
{% 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.
- Numeric
Test given value is numeric (behaviour like PHP 7
is_numeric
).
The test will return false for hexadecimal strings as this will be the default behaviour in PHP 7. (http://php.net/manual/en/function.is-numeric.php)
{{ 12 is numeric ? 'Yes' : 'No' }}
{# Yes #}
{{ '-1.3' is numeric ? 'Yes' : 'No' }}
{# Yes #}
{{ '0x539' is not numeric ? 'Hex. is not numeric' : 'N'}}
{# Hex. is not numeric #}
- json_decode Decode json string, returning php associative arrays. Uses the PHP json_decode function
- Initial release - tags {% break %}, {% continue %} and {% return %}
- Added
numeric
test, fixed parse error in{% return %}
- Fixed loop bug in
{% continue %}
- Possibility of
{% return %}
with no value
- Added json_decode
Brought to you by Marion Newlevant