Add directive @bool to Blade#53179
Conversation
Add @Bool directive functionality to Blade, allowing boolean values to be printed directly into strings or used in object construction.
| */ | ||
| protected function compileBool($condition) | ||
| { | ||
| return "<?php if{$condition}: echo 'true'; else: 'false'; endif; ?>"; |
There was a problem hiding this comment.
Your code doesn't print anything for the "false" path.
return "<?php echo {$condition} ? 'true' : 'false'; ?>";
|
|
||
| class BladeBoolTest extends AbstractBladeTestCase | ||
| { | ||
| public function testBool() |
There was a problem hiding this comment.
This is probably one of the few cases where an eval() test would have found the error above, like so:
public function testCompileBool(): void
{
$someViewVarTruthy = 123;
$compiled = $this->compiler->compileString('@bool($someViewVarTruthy)');
ob_start();
eval(substr($compiled, 6, -3));
$this->assertEquals('true', ob_get_clean());
$someViewVarFalsey = '0';
$compiled = $this->compiler->compileString('@bool($someViewVarFalsey)');
ob_start();
eval(substr($compiled, 6, -3));
$this->assertEquals('false', ob_get_clean());
}There was a problem hiding this comment.
You're right, it didn't consider a Falsey values, i added a Falsey support and a couple of test for NULL and Class instances.
Fix errors that occours when a Falsey value like '0' is passed, also added a NULL and instance testing.
|
btw it works with |
|
@david-valdivia What's the difference between |
no problem, @Bool is thinked only in print a boolean values, means that it could recognize between Falsey and Truthy values o diference with @js that convert into json. @Bool() always returns a boolean value, @js() converts into a json. |
|
He means PR on laravel/docs |
haha, i`m sorry, of course. |


Add @Bool directive functionality to Blade, allowing boolean values to be printed directly into strings or used in object construction.
Examples:
JS
Alpine
Bootstrap: