diff --git a/README.md b/README.md index a217c17..f7315b2 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 %} @@ -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) \ No newline at end of file diff --git a/mntwigperversion/MnTwigPerversionPlugin.php b/mntwigperversion/MnTwigPerversionPlugin.php index aee6a71..6bd1af2 100644 --- a/mntwigperversion/MnTwigPerversionPlugin.php +++ b/mntwigperversion/MnTwigPerversionPlugin.php @@ -68,7 +68,7 @@ public function getReleaseFeedUrl() */ public function getVersion() { - return '1.0.0'; + return '1.0.1'; } /** diff --git a/mntwigperversion/twigextensions/MnTwigPerversionTwigExtension.php b/mntwigperversion/twigextensions/MnTwigPerversionTwigExtension.php index beef524..c3960cc 100644 --- a/mntwigperversion/twigextensions/MnTwigPerversionTwigExtension.php +++ b/mntwigperversion/twigextensions/MnTwigPerversionTwigExtension.php @@ -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 { @@ -39,4 +42,11 @@ public function getTokenParsers() ); } + public function getTests() + { + return array( + new \GeckoPackages\Twig\Tests\NumericTest(), + ); + } + } \ No newline at end of file diff --git a/mntwigperversion/twigextensions/NumericTest.php b/mntwigperversion/twigextensions/NumericTest.php new file mode 100644 index 0000000..12d77e7 --- /dev/null +++ b/mntwigperversion/twigextensions/NumericTest.php @@ -0,0 +1,59 @@ + '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)) + ; + } +} diff --git a/releases.json b/releases.json index 3a4a76a..5c679c5 100644 --- a/releases.json +++ b/releases.json @@ -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 %}" + ] } ] \ No newline at end of file