-
Notifications
You must be signed in to change notification settings - Fork 11k
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
Add directive @bool to Blade #53179
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: