Skip to content

Commit

Permalink
added is array test
Browse files Browse the repository at this point in the history
  • Loading branch information
marionnewlevant committed Dec 8, 2020
1 parent d201d17 commit 7c47292
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Twig Perversion Changelog

## 2.2.0 - 2020.12.07
### Added
- Added `is array` test

## 2.1.0 - 2019.06.09
### Added
- Added `{% while %}` loop.
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Making twig do things it really shouldn't. Twig is not intended to be a gene

- `{% while %}`, `{% break %}`, `{% continue %}`, and `{% return %}` tags
- `===`, `!==`, and `<=>` operators
- `is numeric` and `is string` tests
- `is numeric`, `is string`, and `is array` tests
- `array_splice`, `string`, `float`, `int`, and `bool` filters

## Requirements
Expand Down Expand Up @@ -121,6 +121,18 @@ or
{{ 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 #}
```


Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "marionnewlevant/twig-perversion",
"description": "Making twig do things it really shouldn't",
"version": "2.1.0",
"version": "2.2.0",
"type": "craft-plugin",
"keywords": [
"craft", "craftcms", "plugin", "twigperversion"
Expand Down
10 changes: 10 additions & 0 deletions src/twigextensions/Array_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace marionnewlevant\twigperversion\twigextensions;

class Array_Test extends \Twig_Node_Expression_Test
{
public function compile(\Twig_Compiler $compiler)
{
$compiler->raw('is_array(')->subcompile($this->getNode('node'))->raw(')');
}
}
1 change: 1 addition & 0 deletions src/twigextensions/TwigPerversionTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function getTests()
return [
new \Twig_Test('numeric', null, ['node_class' => '\marionnewlevant\twigperversion\twigextensions\Numeric_Test']),
new \Twig_Test('string', null, ['node_class' => '\marionnewlevant\twigperversion\twigextensions\String_Test']),
new \Twig_Test('array', null, ['node_class' => '\marionnewlevant\twigperversion\twigextensions\Array_Test']),
];
}

Expand Down

0 comments on commit 7c47292

Please sign in to comment.