Skip to content

Commit

Permalink
IfNode: uses AuxiliaryNode to hide usage of $this
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 27, 2023
1 parent 10ca5a3 commit 22c5690
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/Latte/Essential/Nodes/IfNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Latte\Essential\Nodes;

use Latte\CompileException;
use Latte\Compiler\ExpressionBuilder;
use Latte\Compiler\Nodes\AreaNode;
use Latte\Compiler\Nodes\Php\Expression;
use Latte\Compiler\Nodes\Php\ExpressionNode;
Expand Down Expand Up @@ -83,7 +82,10 @@ private static function buildCondition(TagParser $parser): ExpressionNode
$block = $parser->tryConsumeTokenBeforeUnquotedString('block') ?? $parser->stream->tryConsume('#');
$name = $parser->parseUnquotedStringOrExpression();
$list[] = $block || $name instanceof StringNode
? ExpressionBuilder::variable('$this')->method('hasBlock', [$name])->build()
? new Expression\AuxiliaryNode(
fn(PrintContext $context, ExpressionNode $name) => '$this->hasBlock(' . $name->print($context) . ')',
$name,
)
: new Expression\IssetNode([$name]);
} while ($parser->stream->tryConsume(','));

Expand Down
12 changes: 4 additions & 8 deletions tests/tags/ifset.block.nodes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,9 @@ Assert::match(<<<'XX'
If:
BinaryOp:
operator: &&
MethodCall:
Variable:
name: this
Identifier:
name: hasBlock
Argument:
String:
value: foo
Auxiliary:
String:
value: foo
Isset:
Variable:
name: item
Expand All @@ -31,4 +26,5 @@ Assert::match(<<<'XX'
Fragment:
Text:
content: '.else.'

XX, exportTraversing('{ifset foo, $item}.ifset.{else}.else.{/ifset}'));

0 comments on commit 22c5690

Please sign in to comment.