Skip to content

Commit

Permalink
Tag::closestTag() finds by class name
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 9, 2023
1 parent 73f06ec commit 5a1d842
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/Latte/Compiler/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ public function getNotation(bool $withArgs = false): string


/**
* @param string[] $names
* @param class-string[] $classes
*/
public function closestTag(array $names, ?callable $condition = null): ?self
public function closestTag(array $classes, ?callable $condition = null): ?self
{
$tag = $this->parent;
while ($tag && (
!in_array($tag->name, $names, true)
(!in_array($tag->node ? $tag->node::class : null, $classes, true) && !in_array($tag->name, $classes, true))
|| ($condition && !$condition($tag))
)) {
$tag = $tag->parent;
Expand Down
5 changes: 4 additions & 1 deletion src/Latte/Essential/Nodes/IncludeBlockNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ public static function create(Tag $tag, TemplateParser $parser): static
throw new CompileException('Filters are not allowed in {include parent}', $tag->position);

} elseif ($node->parent || $tokenName->is('this')) {
$item = $tag->closestTag(['block', 'define'], fn($item) => isset($item->data->block) && $item->data->block->name !== '');
$item = $tag->closestTag(
[BlockNode::class, DefineNode::class],
fn($item) => isset($item->data->block) && $item->data->block->name !== ''
);
if (!$item) {
throw new CompileException("Cannot include $tokenName->text block outside of any block.", $tag->position);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Latte/Essential/Nodes/IterateWhileNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class IterateWhileNode extends StatementNode
/** @return \Generator<int, ?array, array{AreaNode, ?Tag}, static> */
public static function create(Tag $tag): \Generator
{
$foreach = $tag->closestTag(['foreach']);
$foreach = $tag->closestTag([ForeachNode::class]);
if (!$foreach) {
throw new CompileException("Tag {{$tag->name}} must be inside {foreach} ... {/foreach}.", $tag->position);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Latte/Essential/Nodes/RollbackNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RollbackNode extends StatementNode
{
public static function create(Tag $tag): static
{
if (!$tag->closestTag(['try'])) {
if (!$tag->closestTag([TryNode::class])) {
throw new CompileException('Tag {rollback} must be inside {try} ... {/try}.', $tag->position);
}

Expand Down

0 comments on commit 5a1d842

Please sign in to comment.