diff --git a/README.md b/README.md index 97697a8..d80357e 100644 --- a/README.md +++ b/README.md @@ -35,13 +35,20 @@ MN Twig Perversion works on Craft 2.4.x, Craft 2.5.x, Craft 2.6.x, and probably {# do whatever... #} {% endfor %} -`{% return %}` to return a value from a macro: +`{% 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 @@ -80,4 +87,8 @@ The test will return false for hexadecimal strings as this will be the default b * Fixed loop bug in `{% continue %}` +### 1.0.3 -- 2016.06.20 + +* Possibility of `{% return %}` with no value + Brought to you by [Marion Newlevant](http://marion.newlevant.com) diff --git a/mntwigperversion/MnTwigPerversionPlugin.php b/mntwigperversion/MnTwigPerversionPlugin.php old mode 100644 new mode 100755 index e25b1e2..d4285c2 --- a/mntwigperversion/MnTwigPerversionPlugin.php +++ b/mntwigperversion/MnTwigPerversionPlugin.php @@ -17,112 +17,119 @@ class MnTwigPerversionPlugin extends BasePlugin { - /** - * Returns the user-facing name. - * - * @return mixed - */ - public function getName() - { - return Craft::t('MN Twig Perversion'); - } + /** + * Returns the user-facing name. + * + * @return mixed + */ + public function getName() + { + return Craft::t('MN Twig Perversion'); + } - /** - * Plugins can have descriptions of themselves displayed on the Plugins page by adding a getDescription() method - * on the primary plugin class: - * - * @return mixed - */ - public function getDescription() - { - return Craft::t('Making twig do things it really shouldn\'t'); - } + /** + * Plugins can have descriptions of themselves displayed on the Plugins page by adding a getDescription() method + * on the primary plugin class: + * + * @return mixed + */ + public function getDescription() + { + return Craft::t('Making twig do things it really shouldn\'t'); + } - /** - * Plugins can have links to their documentation on the Plugins page by adding a getDocumentationUrl() method on - * the primary plugin class: - * - * @return string - */ - public function getDocumentationUrl() - { - return 'https://github.com/marionnewlevant/craft-twig_perversion'; - } + /** + * Plugins can have links to their documentation on the Plugins page by adding a getDocumentationUrl() method on + * the primary plugin class: + * + * @return string + */ + public function getDocumentationUrl() + { + return 'https://github.com/marionnewlevant/craft-twig_perversion'; + } - /** - * Plugins can now take part in Craft’s update notifications, and display release notes on the Updates page, by - * providing a JSON feed that describes new releases, and adding a getReleaseFeedUrl() method on the primary - * plugin class. - * - * @return string - */ - public function getReleaseFeedUrl() - { - return 'https://raw.githubusercontent.com/marionnewlevant/craft-twig_perversion/master/releases.json'; - } + /** + * Plugins can now take part in Craft’s update notifications, and display release notes on the Updates page, by + * providing a JSON feed that describes new releases, and adding a getReleaseFeedUrl() method on the primary + * plugin class. + * + * @return string + */ + public function getReleaseFeedUrl() + { + return 'https://raw.githubusercontent.com/marionnewlevant/craft-twig_perversion/master/releases.json'; + } - /** - * Returns the version number. - * - * @return string - */ - public function getVersion() - { - return '1.0.2'; - } + /** + * Returns the version number. + * + * @return string + */ + public function getVersion() + { + return '1.0.3'; + } - /** - * As of Craft 2.5, Craft no longer takes the whole site down every time a plugin’s version number changes, in - * case there are any new migrations that need to be run. Instead plugins must explicitly tell Craft that they - * have new migrations by returning a new (higher) schema version number with a getSchemaVersion() method on - * their primary plugin class: - * - * @return string - */ - public function getSchemaVersion() - { - return '1.0.0'; - } + /** + * As of Craft 2.5, Craft no longer takes the whole site down every time a plugin’s version number changes, in + * case there are any new migrations that need to be run. Instead plugins must explicitly tell Craft that they + * have new migrations by returning a new (higher) schema version number with a getSchemaVersion() method on + * their primary plugin class: + * + * @return string + */ + public function getSchemaVersion() + { + return '1.0.0'; + } - /** - * Returns the developer’s name. - * - * @return string - */ - public function getDeveloper() - { - return 'Marion Newlevant'; - } + /** + * Returns the developer’s name. + * + * @return string + */ + public function getDeveloper() + { + return 'Marion Newlevant'; + } - /** - * Returns the developer’s website URL. - * - * @return string - */ - public function getDeveloperUrl() - { - return 'http://marion.newlevant.com'; - } + /** + * Returns the developer’s website URL. + * + * @return string + */ + public function getDeveloperUrl() + { + return 'http://marion.newlevant.com'; + } - /** - * Returns whether the plugin should get its own tab in the CP header. - * - * @return bool - */ - public function hasCpSection() - { - return false; - } + /** + * Returns whether the plugin should get its own tab in the CP header. + * + * @return bool + */ + public function hasCpSection() + { + return false; + } - /** - * Add any Twig extensions. - * - * @return mixed - */ - public function addTwigExtension() - { - Craft::import('plugins.mntwigperversion.twigextensions.MnTwigPerversionTwigExtension'); + /** + * Add any Twig extensions. + * + * @return mixed + */ + public function addTwigExtension() + { + Craft::import('plugins.mntwigperversion.twigextensions.MnTwigPerversionTwigExtension'); + Craft::import('plugins.mntwigperversion.twigextensions.MnTwigPerversion_Break_Node'); + Craft::import('plugins.mntwigperversion.twigextensions.MnTwigPerversion_Break_TokenParser'); + Craft::import('plugins.mntwigperversion.twigextensions.MnTwigPerversion_Continue_Node'); + Craft::import('plugins.mntwigperversion.twigextensions.MnTwigPerversion_Continue_TokenParser'); + Craft::import('plugins.mntwigperversion.twigextensions.MnTwigPerversion_Return_Node'); + Craft::import('plugins.mntwigperversion.twigextensions.MnTwigPerversion_Return_TokenParser'); + // Craft::import('plugins.mntwigperversion.twigextensions.NumericTest'); - return new MnTwigPerversionTwigExtension(); - } + return new MnTwigPerversionTwigExtension(); + } } diff --git a/mntwigperversion/resources/icon.svg b/mntwigperversion/resources/icon.svg old mode 100644 new mode 100755 diff --git a/mntwigperversion/twigextensions/Break_TokenParser.php b/mntwigperversion/twigextensions/Break_TokenParser.php deleted file mode 100644 index 3df1446..0000000 --- a/mntwigperversion/twigextensions/Break_TokenParser.php +++ /dev/null @@ -1,32 +0,0 @@ -parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); - - return new Break_Node(array(), array(), $token->getLine(), $this->getTag()); - } - - public function getTag() - { - return 'break'; - } - -} - diff --git a/mntwigperversion/twigextensions/Continue_Node.php b/mntwigperversion/twigextensions/Continue_Node.php deleted file mode 100644 index cce87e5..0000000 --- a/mntwigperversion/twigextensions/Continue_Node.php +++ /dev/null @@ -1,39 +0,0 @@ -addDebugInfo($this) - ->write("if (array_key_exists('loop', \$context)) {\n") - ->indent() - ->write("++\$context['loop']['index0'];\n") - ->write("++\$context['loop']['index'];\n") - ->write("\$context['loop']['first'] = false;\n") - ->write("if (isset(\$context['loop']['length'])) {\n") - ->indent() - ->write("--\$context['loop']['revindex0'];\n") - ->write("--\$context['loop']['revindex'];\n") - ->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n") - ->outdent() - ->write("}\n") - ->outdent() - ->write("}\n") - ->write("continue;\n"); - } -} diff --git a/mntwigperversion/twigextensions/Continue_TokenParser.php b/mntwigperversion/twigextensions/Continue_TokenParser.php deleted file mode 100644 index 0022505..0000000 --- a/mntwigperversion/twigextensions/Continue_TokenParser.php +++ /dev/null @@ -1,32 +0,0 @@ -parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); - - return new Continue_Node(array(), array(), $token->getLine(), $this->getTag()); - } - - public function getTag() - { - return 'continue'; - } - -} - diff --git a/mntwigperversion/twigextensions/MnTwigPerversionTwigExtension.php b/mntwigperversion/twigextensions/MnTwigPerversionTwigExtension.php old mode 100644 new mode 100755 index c3960cc..d5e54d2 --- a/mntwigperversion/twigextensions/MnTwigPerversionTwigExtension.php +++ b/mntwigperversion/twigextensions/MnTwigPerversionTwigExtension.php @@ -14,39 +14,36 @@ namespace Craft; -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 { - /** - * Returns the name of the extension. - * - * @return string The extension name - */ - public function getName() - { - return 'MnTwigPerversion'; - } + /** + * Returns the name of the extension. + * + * @return string The extension name + */ + public function getName() + { + return 'MnTwigPerversion'; + } - public function getTokenParsers() - { - return array( - new Break_TokenParser(), - new Continue_TokenParser(), - new Return_TokenParser(), - ); - } + public function getTokenParsers() + { + return array( + new MnTwigPerversion_Break_TokenParser(), + new MnTwigPerversion_Continue_TokenParser(), + new MnTwigPerversion_Return_TokenParser(), + ); + } - public function getTests() - { - return array( - new \GeckoPackages\Twig\Tests\NumericTest(), - ); - } + public function getTests() + { + return array( + new \GeckoPackages\Twig\Tests\NumericTest(), + ); + } -} \ No newline at end of file +} diff --git a/mntwigperversion/twigextensions/Break_Node.php b/mntwigperversion/twigextensions/MnTwigPerversion_Break_Node.php old mode 100644 new mode 100755 similarity index 52% rename from mntwigperversion/twigextensions/Break_Node.php rename to mntwigperversion/twigextensions/MnTwigPerversion_Break_Node.php index 64e7f3b..54393d7 --- a/mntwigperversion/twigextensions/Break_Node.php +++ b/mntwigperversion/twigextensions/MnTwigPerversion_Break_Node.php @@ -11,15 +11,15 @@ * @link https://github.com/marionnewlevant/craft-twig_perversion */ -class Break_Node extends \Twig_Node +class MnTwigPerversion_Break_Node extends \Twig_Node { - /** - * Compiles a Break_Node into PHP. - */ - public function compile(\Twig_Compiler $compiler) - { - $compiler - ->addDebugInfo($this) - ->write("break;\n"); - } + /** + * Compiles a Break_Node into PHP. + */ + public function compile(\Twig_Compiler $compiler) + { + $compiler + ->addDebugInfo($this) + ->write("break;\n"); + } } diff --git a/mntwigperversion/twigextensions/MnTwigPerversion_Break_TokenParser.php b/mntwigperversion/twigextensions/MnTwigPerversion_Break_TokenParser.php new file mode 100755 index 0000000..aacc95b --- /dev/null +++ b/mntwigperversion/twigextensions/MnTwigPerversion_Break_TokenParser.php @@ -0,0 +1,30 @@ +parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); + + return new MnTwigPerversion_Break_Node(array(), array(), $token->getLine(), $this->getTag()); + } + + public function getTag() + { + return 'break'; + } + +} + diff --git a/mntwigperversion/twigextensions/MnTwigPerversion_Continue_Node.php b/mntwigperversion/twigextensions/MnTwigPerversion_Continue_Node.php new file mode 100755 index 0000000..b55c80e --- /dev/null +++ b/mntwigperversion/twigextensions/MnTwigPerversion_Continue_Node.php @@ -0,0 +1,39 @@ +addDebugInfo($this) + ->write("if (array_key_exists('loop', \$context)) {\n") + ->indent() + ->write("++\$context['loop']['index0'];\n") + ->write("++\$context['loop']['index'];\n") + ->write("\$context['loop']['first'] = false;\n") + ->write("if (isset(\$context['loop']['length'])) {\n") + ->indent() + ->write("--\$context['loop']['revindex0'];\n") + ->write("--\$context['loop']['revindex'];\n") + ->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n") + ->outdent() + ->write("}\n") + ->outdent() + ->write("}\n") + ->write("continue;\n"); + } +} diff --git a/mntwigperversion/twigextensions/MnTwigPerversion_Continue_TokenParser.php b/mntwigperversion/twigextensions/MnTwigPerversion_Continue_TokenParser.php new file mode 100755 index 0000000..1d944cf --- /dev/null +++ b/mntwigperversion/twigextensions/MnTwigPerversion_Continue_TokenParser.php @@ -0,0 +1,30 @@ +parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); + + return new MnTwigPerversion_Continue_Node(array(), array(), $token->getLine(), $this->getTag()); + } + + public function getTag() + { + return 'continue'; + } + +} + diff --git a/mntwigperversion/twigextensions/MnTwigPerversion_Return_Node.php b/mntwigperversion/twigextensions/MnTwigPerversion_Return_Node.php new file mode 100755 index 0000000..138757f --- /dev/null +++ b/mntwigperversion/twigextensions/MnTwigPerversion_Return_Node.php @@ -0,0 +1,36 @@ +addDebugInfo($this) + ->write("ob_end_clean();\n") + ->write('return '); + if ($this->hasNode('expr')) { + $compiler->subcompile($this->getNode('expr')); + } + else + { + $compiler->raw('""'); + } + + $compiler->raw(";\n"); + } +} diff --git a/mntwigperversion/twigextensions/MnTwigPerversion_Return_TokenParser.php b/mntwigperversion/twigextensions/MnTwigPerversion_Return_TokenParser.php new file mode 100755 index 0000000..cdb3914 --- /dev/null +++ b/mntwigperversion/twigextensions/MnTwigPerversion_Return_TokenParser.php @@ -0,0 +1,38 @@ +parser->getStream(); // entire stream of tokens + $nodes = array(); + + if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) + { + $nodes['expr'] = $this->parser->getExpressionParser()->parseExpression(); + } + + $stream->expect(\Twig_Token::BLOCK_END_TYPE); + + return new MnTwigPerversion_Return_Node($nodes, array(), $token->getLine(), $this->getTag()); + } + + public function getTag() + { + return 'return'; + } + +} + diff --git a/mntwigperversion/twigextensions/NumericTest.php b/mntwigperversion/twigextensions/NumericTest.php old mode 100644 new mode 100755 diff --git a/mntwigperversion/twigextensions/Return_Node.php b/mntwigperversion/twigextensions/Return_Node.php deleted file mode 100644 index 1d52a44..0000000 --- a/mntwigperversion/twigextensions/Return_Node.php +++ /dev/null @@ -1,34 +0,0 @@ - $expr), array(), $lineno, $tag); - } - - /** - * Compiles a Return_Node into PHP. - */ - public function compile(\Twig_Compiler $compiler) - { - $compiler - ->addDebugInfo($this) - ->write("ob_end_clean();\n") - ->write('return ') - ->subcompile($this->getNode('expr')) - ->raw(";\n") - ; - } -} diff --git a/mntwigperversion/twigextensions/Return_TokenParser.php b/mntwigperversion/twigextensions/Return_TokenParser.php deleted file mode 100644 index 4c110e2..0000000 --- a/mntwigperversion/twigextensions/Return_TokenParser.php +++ /dev/null @@ -1,34 +0,0 @@ -parser->getExpressionParser()->parseExpression(); - - $this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE); - - return new Return_Node($expr, $token->getLine(), $this->getTag()); - } - - public function getTag() - { - return 'return'; - } - -} - diff --git a/releases.json b/releases.json index 97724a8..29a04b7 100644 --- a/releases.json +++ b/releases.json @@ -23,5 +23,14 @@ "notes": [ "[Fixed] Loop bug in {% continue %}" ] + }, + { + "version": "1.0.3", + "downloadUrl": "https://github.com/marionnewlevant/craft-twig_perversion/archive/v1.0.3.zip", + "date": "2016-06-20T20:37:12.921Z", + "notes": [ + "[Added] {% return %} does not require a value to return", + "[Fixed] Renamed classes to prevent possible name conflicts" + ] } ]