Skip to content

Commit

Permalink
[5.x] Support as on nav tag (#11522)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell authored Mar 3, 2025
1 parent 8df1e58 commit a691c1e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Tags/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ protected function structure($handle)
'max_depth' => $this->params->get('max_depth'),
]);

return $this->toArray($tree);
$value = $this->toArray($tree);

if ($this->parser && ($as = $this->params->get('as'))) {
return [$as => $value];
}

return $value;
}

protected function ensureStructureExists(string $handle): void
Expand Down
42 changes: 42 additions & 0 deletions tests/Tags/StructureTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,48 @@ public function it_renders_a_nav_with_scope()
]));
}

#[Test]
public function it_renders_a_nav_with_as()
{
$this->createCollectionAndNav();

// The html uses <i> tags (could be any tag, but i is short) to prevent whitespace comparison issues in the assertion.
$template = <<<'EOT'
<ul>
{{ nav:test as="navtastic" }}
<li>Something before the loop</li>
{{ navtastic }}
<li>
<i>{{ nav_title or title }} {{ foo }}</i>
</li>
{{ /navtastic }}
{{ /nav:test }}
</ul>
EOT;

$expected = <<<'EOT'
<ul>
<li>Something before the loop</li>
<li>
<i>Navtitle One bar</i>
</li>
<li>
<i>Two notbar</i>
</li>
<li>
<i>Three bar</i>
</li>
<li>
<i>Title only bar</i>
</li>
</ul>
EOT;

$this->assertXmlStringEqualsXmlString($expected, (string) Antlers::parse($template, [
'foo' => 'bar', // to test that cascade is inherited.
]));
}

#[Test]
public function it_hides_unpublished_entries_by_default()
{
Expand Down

0 comments on commit a691c1e

Please sign in to comment.