Coder Social home page Coder Social logo

peteeveleigh / craft-twig_perversion Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marionnewlevant/craft-twig_perversion

0.0 0.0 0.0 48 KB

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: MIT License

PHP 100.00%

craft-twig_perversion's Introduction

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.

  • {% 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.

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

  • {% while %} loop:

    {% 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.

  • {% break %} to exit a for loop or while loop:

    {% 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:

    {% 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 from 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)

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

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

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

    Test whether given value is a string.

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

{{ 12 is string  ? 'Yes' : 'No' }}
{# No #}
{```

- **Array**

  Test whether given value is an array.
  
```Twig
{ [] is array ? 'Yes' : 'No' }}
{# Yes #}

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

Filters

  • 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

craft-twig_perversion's People

Contributors

marionnewlevant avatar brandonkelly avatar lindseydiloreto avatar mooreds avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.