Skip to content

Making twig do things it really shouldn't. while, break, continue, and return tags plus tests, operators, and filters that probably don't belong in the language

License

Notifications You must be signed in to change notification settings

marionnewlevant/craft-twig_perversion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MN 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
  • is numeric test
  • json_decode filter

Installation

To install MN Twig Perversion, follow these steps:

  1. Download & unzip the file and place the mntwigperversion directory into your craft/plugins directory
  2. 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.

Using MN 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.

Tests

  • 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)

Examples

{{ 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 #}

Filters

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

MN Twig Perversion Changelog

1.0.0 -- 2016.04.17

  • Initial release - tags {% break %}, {% continue %} and {% return %}

1.0.1 -- 2016.05.06

  • Added numeric test, fixed parse error in {% return %}

1.0.2 -- 2016.06.17

  • Fixed loop bug in {% continue %}

1.0.3 -- 2016.06.20

  • Possibility of {% return %} with no value

1.1.0 -- 2016.07.08

  • Added json_decode

Brought to you by Marion Newlevant

About

Making twig do things it really shouldn't. while, break, continue, and return tags plus tests, operators, and filters that probably don't belong in the language

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •