-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add directive @Bool to Blade Add @Bool directive functionality to Blade, allowing boolean values to be printed directly into strings or used in object construction. * Fix @Bool suggestion Fix errors that occours when a Falsey value like '0' is passed, also added a NULL and instance testing. * continuous-integration * Update BladeBoolTest.php * Update BladeBoolTest.php * Update CompilesConditionals.php * Update CompilesConditionals.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
- Loading branch information
1 parent
9867436
commit 5c9cb78
Showing
2 changed files
with
82 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\View\Blade; | ||
|
||
class BladeBoolTest extends AbstractBladeTestCase | ||
{ | ||
public function testBool() | ||
{ | ||
// For Javascript object{'isBool' : true} | ||
$string = "{'isBool' : @bool(true)}"; | ||
$expected = "{'isBool' : <?php echo ((true) ? 'true' : 'false'); ?>}"; | ||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
|
||
// For Javascript object{'isBool' : false} | ||
$string = "{'isBool' : @bool(false)}"; | ||
$expected = "{'isBool' : <?php echo ((false) ? 'true' : 'false'); ?>}"; | ||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
|
||
// For Alpine.js x-show attribute | ||
$string = "<input type='text' x-show='@bool(true)' />"; | ||
$expected = "<input type='text' x-show='<?php echo ((true) ? 'true' : 'false'); ?>' />"; | ||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
|
||
// For Alpine.js x-show attribute | ||
$string = "<input type='text' x-show='@bool(false)' />"; | ||
$expected = "<input type='text' x-show='<?php echo ((false) ? 'true' : 'false'); ?>' />"; | ||
$this->assertEquals($expected, $this->compiler->compileString($string)); | ||
} | ||
|
||
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()); | ||
|
||
$anotherSomeViewVarTruthy = new SomeClass(); | ||
$compiled = $this->compiler->compileString('@bool($anotherSomeViewVarTruthy)'); | ||
|
||
ob_start(); | ||
eval(substr($compiled, 6, -3)); | ||
$this->assertEquals('true', ob_get_clean()); | ||
|
||
$anotherSomeViewVarFalsey = null; | ||
$compiled = $this->compiler->compileString('@bool($anotherSomeViewVarFalsey)'); | ||
|
||
ob_start(); | ||
eval(substr($compiled, 6, -3)); | ||
$this->assertEquals('false', ob_get_clean()); | ||
} | ||
} | ||
|
||
class SomeClass | ||
{ | ||
public function someMethod() | ||
{ | ||
} | ||
} |