-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v2.1.0 - added {% while %} and is string. Removed json_decode
- Loading branch information
1 parent
8644b70
commit d201d17
Showing
7 changed files
with
266 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
namespace marionnewlevant\twigperversion\twigextensions; | ||
|
||
class String_Test extends \Twig_Node_Expression_Test | ||
{ | ||
public function compile(\Twig_Compiler $compiler) | ||
{ | ||
$compiler->raw('is_string(')->subcompile($this->getNode('node'))->raw(')'); | ||
} | ||
} |
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,80 @@ | ||
<?php | ||
|
||
/** | ||
* Represents a while node. | ||
* | ||
* @package TwigPerversion | ||
* @author Marion Newlevant | ||
* @copyright Copyright (c) 2019, Marion Newlevant | ||
* @license MIT | ||
* @link https://github.com/marionnewlevant/craft-twig_perversion | ||
* | ||
*/ | ||
namespace marionnewlevant\twigperversion\twigextensions; | ||
|
||
use Twig\Node\ForLoopNode; | ||
use Twig\Node\Node; | ||
use Twig\Compiler; | ||
|
||
class While_Node extends Node | ||
{ | ||
private $loop; | ||
|
||
public function __construct(Node $condition, Node $body, int $lineno, string $tag = null) | ||
{ | ||
$body = new Node([$body, $this->loop = new ForLoopNode($lineno, $tag)]); | ||
$nodes = [ | ||
'body' => $body, | ||
'condition' => $condition | ||
]; | ||
parent::__construct($nodes, ['with_loop' => true], $lineno, $tag); | ||
} | ||
|
||
/** | ||
* Compiles the node to PHP. | ||
* | ||
* @param Twig_Compiler $compiler | ||
* | ||
* @return void | ||
* | ||
* @see \Twig_Node::compile | ||
*/ | ||
public function compile(Compiler $compiler) | ||
{ | ||
$compiler | ||
->addDebugInfo($this) | ||
->write("\$context['_parent'] = \$context;\n"); | ||
|
||
if ($this->getAttribute('with_loop')) { | ||
$compiler | ||
->write("\$context['loop'] = [\n") | ||
->write(" 'parent' => \$context['_parent'],\n") | ||
->write(" 'index0' => 0,\n") | ||
->write(" 'index' => 1,\n") | ||
->write(" 'first' => true,\n") | ||
->write("];\n") | ||
; | ||
} | ||
|
||
$this->loop->setAttribute('with_loop', $this->getAttribute('with_loop')); | ||
|
||
$compiler | ||
->write('while (') | ||
->subcompile($this->getNode('condition')) | ||
->raw(") {\n") | ||
->indent() | ||
// this is a ForLoopNode, so it updates the loop stuff | ||
->subcompile($this->getNode('body')) | ||
->outdent() | ||
->write("}\n") | ||
; | ||
|
||
$compiler->write("\$_parent = \$context['_parent'];\n"); | ||
|
||
// remove some "private" loop variables (needed for nested loops) | ||
$compiler->write('unset($context[\'_parent\'], $context[\'loop\']);'."\n"); | ||
|
||
// keep the values set in the inner context for variables defined in the outer context | ||
$compiler->write("\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n"); | ||
} | ||
} |
Oops, something went wrong.
d201d17
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix: 'break' not in the 'loop' or 'switch' context