Skip to content

[5.x] Fix issues with Blade nav tag compiler #11872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions src/Providers/ViewServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\View as ViewFactory;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Illuminate\View\View;
use Statamic\Contracts\View\Antlers\Parser as ParserContract;
use Statamic\Facades\Site;
Expand Down Expand Up @@ -178,18 +177,17 @@ public function registerBladeDirectives()
$nested = '$children';
}

$recursiveChildren = <<<'PHP'
@include('compiled__views::'.$__currentStatamicNavView, array_merge(get_defined_vars(), [
'depth' => ($depth ?? 0) + 1,
'__statamicOverrideTagResultValue' => #varName#,
]))
return <<<PHP
<?php
echo \$___statamicNavCallback(
array_merge(get_defined_vars(), [
'depth' => (\$depth ?? 0) + 1,
'__statamicOverrideTagResultValue' => $nested,
]),
\$___statamicNavCallback
);
?>
PHP;

$recursiveChildren = Str::swap([
'#varName#' => $nested,
], $recursiveChildren);

return Blade::compileString($recursiveChildren);
});

}
Expand Down
15 changes: 11 additions & 4 deletions src/View/Blade/Concerns/CompilesNavs.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ protected function compileNav(ComponentNode $component): string
$viewName = '___nav'.sha1($component->outerDocumentContent);

$compiled = (new StatamicTagCompiler())
->prependCompiledContent('$__currentStatamicNavView = \''.$viewName.'\';')
->appendCompiledContent('unset($__currentStatamicNavView);')
->setInterceptNav(false)
->compile($component->outerDocumentContent);

file_put_contents(storage_path('framework/views/'.$viewName.'.blade.php'), $compiled);
return <<<PHP
<?php \$___statamicNavCallback = function (\$___scope, \$___statamicNavCallback) {
extract(\$___scope);
ob_start();
?>$compiled<?php
return ob_get_clean();
};

return '@include(\'compiled__views::'.$viewName.'\', get_defined_vars())';
echo \$___statamicNavCallback(get_defined_vars(), \$___statamicNavCallback);
unset(\$___statamicNavCallback);
?>
PHP;
}
}
59 changes: 59 additions & 0 deletions tests/View/Blade/AntlersComponents/NavCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,63 @@ public function it_renders_aliased_recursive_children()
Blade::render($template)
);
}

#[Test]
public function it_supports_imported_classes_and_functions()
{
$template = <<<'BLADE'
@use (Statamic\Support\Html)

<ul>
<s:nav:main as="the_items">
@foreach ($the_items as $item)
<li>{{ Html::entities($item['title']) }}</li>
@endforeach
</s:nav:main>
</ul>
BLADE;

$expected = <<<'EXPECTED'
<ul>
<li>Home</li>
<li>About</li>
<li>Projects</li>
<li>Contact</li>
</ul>
EXPECTED;

$this->assertSame(
$expected,
Blade::render($template),
);
}

#[Test]
public function it_doesnt_mangle_php_inside_nav_tag()
{
$template = <<<'BLADE'
<ul>
<s:nav:main as="the_items">
@foreach ($the_items as $item)
@php $theValue = $item['title'].'-value'; @endphp
<li>{{ $theValue }}</li>
@endforeach
</s:nav:main>
</ul>
BLADE;

$expected = <<<'EXPECTED'
<ul>
<li>Home-value</li>
<li>About-value</li>
<li>Projects-value</li>
<li>Contact-value</li>
</ul>
EXPECTED;

$this->assertSame(
$expected,
Blade::render($template),
);
}
}