-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Version 5.0.1 - twig 3.12 fixes (Brandon)
- added twig test code - Brandon’s twig 3.12 breaking {% return %} fix
- Loading branch information
1 parent
23985a4
commit 4db6047
Showing
9 changed files
with
279 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
namespace marionnewlevant\twigperversion\twigextensions; | ||
|
||
use marionnewlevant\twigperversion\Plugin; | ||
use Twig\Compiler; | ||
use Twig\Node\Expression\AbstractExpression; | ||
|
||
/** | ||
* Twig Perversion | ||
* | ||
* @package TwigPerversion | ||
* @author Marion Newlevant | ||
* @copyright Copyright (c) 2024, Marion Newlevant | ||
* @license MIT | ||
* @link https://github.com/marionnewlevant/craft-twig_perversion | ||
* @since 2.3.0 | ||
*/ | ||
|
||
class MacroProcessor_Node extends AbstractExpression | ||
{ | ||
public function compile(Compiler $compiler): void | ||
{ | ||
$compiler | ||
->raw("\n") | ||
->indent() | ||
->write(sprintf("%s::processMacroResponse(\n", Plugin::class)) | ||
->indent() | ||
->write('') | ||
->subcompile($this->getNode('methodCallExpression')) | ||
->raw("\n") | ||
->outdent() | ||
->write(")\n") | ||
->outdent() | ||
->write(''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
namespace marionnewlevant\twigperversion\twigextensions; | ||
|
||
use Twig\Environment; | ||
use Twig\Node\Expression\MethodCallExpression; | ||
use Twig\Node\Node; | ||
use Twig\NodeVisitor\NodeVisitorInterface; | ||
|
||
/** | ||
* Twig Perversion | ||
* | ||
* @package TwigPerversion | ||
* @author Marion Newlevant | ||
* @copyright Copyright (c) 2024, Marion Newlevant | ||
* @license MIT | ||
* @link https://github.com/marionnewlevant/craft-twig_perversion | ||
* @since 2.3.0 | ||
*/ | ||
class Return_NodeVisitor implements NodeVisitorInterface | ||
{ | ||
public function enterNode(Node $node, Environment $env): Node | ||
{ | ||
return $node; | ||
} | ||
|
||
public function leaveNode(Node $node, Environment $env): ?Node | ||
{ | ||
if ($node instanceof MethodCallExpression && !$node->getAttribute('is_defined_test')) { | ||
return new MacroProcessor_Node([ | ||
'methodCallExpression' => $node, | ||
], [ | ||
'is_generator' => $node->hasAttribute('is_generator') ? $node->getAttribute('is_generator') : false, | ||
]); | ||
} | ||
return $node; | ||
} | ||
|
||
public function getPriority() | ||
{ | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<h1> Twig-perversion tests</h1> | ||
<p>twig code for testing twig-perversion</p> | ||
|
||
<h2>return</h2> | ||
{% macro testMacro() %} | ||
{% return "test" %} | ||
ignore me... | ||
{% endmacro %} | ||
|
||
{% set x = _self.testMacro() %} | ||
|
||
x = {{x}} | ||
{# x = test #} | ||
<hr> | ||
{% macro t2(v) %} | ||
{% if v < 0 %} | ||
{% return "negative" %} | ||
ignore me | ||
{% else %} | ||
{% return "positive" %} | ||
{% endif %} | ||
ignore me | ||
{% endmacro %} | ||
|
||
negative: {{ _self.t2(-4)}} | ||
positive: {{ _self.t2(4)}} | ||
|
||
<hr> | ||
{% macro double(n) %} | ||
{% return n * 2 %} | ||
ignore me | ||
{% endmacro %} | ||
double(4): {{_self.double(4)}} | ||
|
||
{% macro triple(n) %} | ||
{% return n + _self.double(n) %} | ||
ignore me | ||
{% endmacro %} | ||
triple(4): {{_self.triple(4)}} | ||
<hr> | ||
{% macro recur(n) %} | ||
{{n}} | ||
{% if n > 1 %} | ||
{{ _self.recur(n-1)}} | ||
{% endif %} | ||
{% endmacro %} | ||
|
||
{{ _self.recur(5)}} | ||
<hr> | ||
{% macro fact(n) %} | ||
{% if n == 1 %} | ||
{% return 1 %} | ||
{% endif %} | ||
{% return n * _self.fact(n-1) %} | ||
{% endmacro %} | ||
|
||
1!: {{ _self.fact(1)}} | ||
{% set fact5 = _self.fact(5) %} | ||
5! = {{fact5}} | ||
|
||
<h2>while loops</h2> | ||
while loop indexes<br> | ||
{% set x = 0 %} | ||
{% while x < 5 %} | ||
{{x}} loop: {{loop.index}} {{loop.index0}} {{loop.first ? 'first' : 'not first'}}<br> | ||
{% set x = x + 1 %} | ||
{% endwhile %} | ||
<hr> | ||
while with loop and loop.parent indexes<br> | ||
{% set x = 0 %} | ||
{% while x < 5 %} | ||
{% set y = 0 %} | ||
{% while y < 3 %} | ||
loop: {{loop.index}} {{loop.index0}} {{loop.first ? 'first' : 'not first'}} | ||
parent: {{loop.parent.loop.index}} {{loop.parent.loop.index0}} {{loop.parent.loop.first ? 'first': 'not'}}<br> | ||
{% set y = y + 1 %} | ||
{% endwhile %} | ||
{% set x = x + 1 %} | ||
{% endwhile %} | ||
<hr> | ||
while w/ continue on even: | ||
{% set x = 0 %} | ||
{% while x < 5 %} | ||
{% set x = x + 1 %} | ||
{{x}} | ||
{% if x % 2 == 0 %} {% continue %} {% endif %} | ||
odd | ||
{% endwhile %} | ||
<hr> | ||
while w/ break on 3: | ||
{% set x = 0 %} | ||
{% while x < 5 %} | ||
{{x}} | ||
{% set x = x + 1 %} | ||
{% if x == 3 %} {% break %} {% endif %} | ||
.. | ||
{% endwhile %} | ||
<hr> | ||
|
||
<h4>numeric</h4> | ||
12 ? {{ 12 is numeric ? 'Yes' : 'No' }} | ||
{# Yes #} | ||
|
||
-1.3 ? {{ '-1.3' is numeric ? 'Yes' : 'No' }} | ||
{# Yes #} | ||
|
||
0x539 ? {{ '0x539' is numeric ? 'Yes' : 'No'}} | ||
{# No #} | ||
|
||
<h4>return types</h4> | ||
{% macro foo() %} | ||
{# ... calculate someValue ... #} | ||
{% set someValue = 'our return value' %} | ||
{% return someValue %} | ||
{% endmacro %} | ||
|
||
{% macro bar() %} | ||
{% set x = 0 %} | ||
{% while x < 5 %} | ||
x = {{x}} | ||
{% set x = x + 1 %} | ||
{% endwhile %} | ||
{% return %} | ||
{% endmacro %} | ||
|
||
{% macro baz() %} | ||
{% return 23 %} | ||
{% endmacro %} | ||
|
||
{{ _self.foo() }} | ||
{{ _self.foo() is string ? 'foo is string' : 'foo is not string' }}<br> | ||
|
||
{{ _self.bar() }} | ||
{{ _self.bar() is string ? 'bar is string' : 'bar is not string' }}<br> | ||
|
||
{{ _self.baz() }} | ||
{{ _self.baz() is string ? 'baz is string' : 'baz is not string' }}<br> |