8000 [12.x] Add `@​context` Blade directive by martinbean · Pull Request #56146 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] Add @​context Blade directive #56146

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
Jul 7, 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
Add @​context Blade directive
  • Loading branch information
martinbean committed Jun 26, 2025
commit cbafd0d27a5079405fd01f8a1b460f21b1ad88bc
1 change: 1 addition & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BladeCompiler extends Compiler implements CompilerInterface
Concerns\CompilesComments,
Concerns\CompilesComponents,
Concerns\CompilesConditionals,
Concerns\CompilesContexts,
Concerns\CompilesEchos,
Concerns\CompilesErrors,
Concerns\CompilesFragments,
Expand Down
37 changes: 37 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesContexts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesContexts
{
/**
* Compile the context statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileContext($expression)
{
$expression = $this->stripParentheses($expression);

return '<?php $__contextArgs = ['.$expression.'];
if (context()->has($__contextArgs[0])) :
if (isset($value)) { $__contextPrevious[] = $value; }
$value = context()->get($__contextArgs[0]); ?>';
}

/**
* Compile the endcontext statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEndcontext($expression)
{
return '<?php unset($value);
if (isset($__contextPrevious) && !empty($__contextPrevious)) { $value = array_pop($__contextPrevious); }
if (isset($__contextPrevious) && empty($__contextPrevious)) { unset($__contextPrevious); }
endif;
unset($__contextArgs); ?>';
}
}
27 changes: 27 additions & 0 deletions tests/View/Blade/BladeContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Illuminate\Tests\View\Blade;

class BladeContextTest extends AbstractBladeTestCase
{
public function testContextsAreCompiled()
{
$string = '
@context(\'foo\')
<span>{{ $value }}</span>
@endcontext';
$expected = '
<?php $__contextArgs = [\'foo\'];
if (context()->has($__contextArgs[0])) :
if (isset($value)) { $__contextPrevious[] = $value; }
$value = context()->get($__contextArgs[0]); ?>
<span><?php echo e($value); ?></span>
<?php unset($value);
if (isset($__contextPrevious) && !empty($__contextPrevious)) { $value = array_pop($__contextPrevious); }
if (isset($__contextPrevious) && empty($__contextPrevious)) { unset($__contextPrevious); }
endif;
unset($__contextArgs); ?>';

$this->assertEquals($expected, $this->compiler->compileString($string));
}
}
0