Skip to content
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

New when() helper. #52665

Merged
merged 15 commits into from
Sep 11, 2024
20 changes: 20 additions & 0 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -1047,3 +1047,23 @@ function view($view = null, $data = [], $mergeData = [])
return $factory->make($view, $data, $mergeData);
}
}

if (! function_exists('when')) {
/**
* Output a value if the given condition is true.
*
* @param bool $condition
* @param string $output,
* @param bool $escape
* @return void
*
* @throws \Symfony\Component\HttpKernel\Exception\HttpException
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
danmatthews marked this conversation as resolved.
Show resolved Hide resolved
*/
function when($condition, $output, $escape = false)
{
if ($condition) {
echo $escape ? e($output) : $output;
danmatthews marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
1 change: 1 addition & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class BladeCompiler extends Compiler implements CompilerInterface
{
use Concerns\CompilesAuthorizations,
Concerns\CompilesClasses,
Concerns\CompilesWhens,
Concerns\CompilesComments,
Concerns\CompilesComponents,
Concerns\CompilesConditionals,
Expand Down
21 changes: 21 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesWhens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesWhens
{
/**
* Compile the 'when' statement into valid PHP.
*
* @param string $expression
* @param string $output
* @param bool $escape
* @return string
*/
protected function compileWhen($expression)
danmatthews marked this conversation as resolved.
Show resolved Hide resolved
{
$expression = $expression == '()' ? "(false,'',false)" : $expression;

return "<?php when{$expression}; ?>";
danmatthews marked this conversation as resolved.
Show resolved Hide resolved
}
}
20 changes: 20 additions & 0 deletions tests/View/Blade/BladeWhenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Illuminate\Tests\View\Blade;

class BladeWhenTest extends AbstractBladeTestCase
{
public function testWhenStatmentsAreCompiledRaw()
{
$string = "<span @when(true, 'disabled=\"disabled\"', false)></span>";
$expected = "<span <?php when(true, 'disabled=\"disabled\"', false); ?>></span>";
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testWhenStatementsAreCompiledEmpty()
{
$string = '<span @when()></span>';
$expected = "<span <?php when(false,'',false); ?>></span>";
$this->assertEquals($expected, $this->compiler->compileString($string));
}
}