Skip to content

Commit

Permalink
added numeric test and bumped version
Browse files Browse the repository at this point in the history
  • Loading branch information
marionnewlevant committed May 6, 2016
1 parent 78208f5 commit 283bc9c
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 2 deletions.
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# 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. Tis plugin adds a few of those things anyway. Provides {% break %}, {% continue %}, and {% return %} tags for twig.
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

## Installation

Expand All @@ -13,6 +16,8 @@ MN Twig Perversion works on Craft 2.4.x, Craft 2.5.x, Craft 2.6.x, and probably

## Using MN Twig Perversion

### Tags

`{% break %}` to exit a for loop:

{% for straw in haystack %}
Expand All @@ -39,10 +44,36 @@ MN Twig Perversion works on Craft 2.4.x, Craft 2.5.x, Craft 2.6.x, and probably

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

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

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

Brought to you by [Marion Newlevant](http://marion.newlevant.com)
2 changes: 1 addition & 1 deletion mntwigperversion/MnTwigPerversionPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function getReleaseFeedUrl()
*/
public function getVersion()
{
return '1.0.0';
return '1.0.1';
}

/**
Expand Down
10 changes: 10 additions & 0 deletions mntwigperversion/twigextensions/MnTwigPerversionTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
require_once('Break_TokenParser.php');
require_once('Continue_TokenParser.php');
require_once('Return_TokenParser.php');
/* NumericTest is lifted straight out of https://github.com/GeckoPackages/GeckoTwig
*/
require_once('NumericTest.php');

class MnTwigPerversionTwigExtension extends \Twig_Extension
{
Expand All @@ -39,4 +42,11 @@ public function getTokenParsers()
);
}

public function getTests()
{
return array(
new \GeckoPackages\Twig\Tests\NumericTest(),
);
}

}
59 changes: 59 additions & 0 deletions mntwigperversion/twigextensions/NumericTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the GeckoPackages.
*
* (c) GeckoPackages https://github.com/GeckoPackages
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace GeckoPackages\Twig\Tests;

/**
* Test if a given value is "numeric".
*
* The test will return false for hexadecimal strings as this will be
* the default behaviour in PHP 7.
*
* @see http://php.net/manual/en/function.is-numeric.php
*
* @api
*
* @author SpacePossum
*/
class NumericTest extends \Twig_SimpleTest
{
public function __construct()
{
parent::__construct(
'numeric',
null,
array('node_class' => 'GeckoPackages\Twig\Tests\NumericTestNode')
);
}
}

final class NumericTestNode extends \Twig_Node_Expression_Test
{
public function compile(\Twig_Compiler $compiler)
{
if (PHP_VERSION_ID >= 70000) {
$compiler->raw('is_numeric(')->subcompile($this->getNode('node'))->raw(')');

return;
}

// Forward support towards PHP 7.
// Strings in hexadecimal notation are no longer regarded as `numeric`,
// for example: `is_numeric('0xf4c3b00c')` returns `false`.
// @see http://php.net/manual/en/function.is-numeric.php
$var = $compiler->getVarName();
$compiler
->raw(sprintf('(is_numeric($%s = ', $var))
->subcompile($this->getNode('node'))
->raw(sprintf(') && (!is_string($%s) || (strlen($%s) < 2 || (\'x\' !== $%s[1] && \'X\' !== $%s[1]))))', $var, $var, $var, $var))
;
}
}
9 changes: 9 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,14 @@
"notes": [
"[Added] Initial release, with break, continue, and return"
]
},
{
"version": "1.0.1",
"downloadUrl": "https://github.com/marionnewlevant/craft-twig_perversion/archive/v1.0.1.zip",
"date": "2016-05-06T20:37:12.921Z",
"notes": [
"[Added] Numeric Test (from https://github.com/GeckoPackages/GeckoTwig)",
"[Fixed] Parse error on {% return %}"
]
}
]

0 comments on commit 283bc9c

Please sign in to comment.