Skip to content

Commit

Permalink
Add directive @Bool to Blade (#53179)
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
dvaldiviacom and taylorotwell authored Oct 16, 2024
1 parent 9867436 commit 5c9cb78
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,14 @@ public function compileEndOnce()
}

/**
* Compile a selected block into valid PHP.
* Compile a boolean value into a raw true / false value for embedding into HTML attributes or JavaScript.
*
* @param string $condition
* @param bool $condition
* @return string
*/
protected function compileSelected($condition)
protected function compileBool($condition)
{
return "<?php if{$condition}: echo 'selected'; endif; ?>";
return "<?php echo ($condition ? 'true' : 'false'); ?>";
}

/**
Expand Down Expand Up @@ -360,6 +360,17 @@ protected function compileReadonly($condition)
return "<?php if{$condition}: echo 'readonly'; endif; ?>";
}

/**
* Compile a selected block into valid PHP.
*
* @param string $condition
* @return string
*/
protected function compileSelected($condition)
{
return "<?php if{$condition}: echo 'selected'; endif; ?>";
}

/**
* Compile the push statements into valid PHP.
*
Expand Down
67 changes: 67 additions & 0 deletions tests/View/Blade/BladeBoolTest.php
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()
{
}
}

0 comments on commit 5c9cb78

Please sign in to comment.