Skip to content

Commit 5c9cb78

Browse files
Add directive @Bool to Blade (#53179)
* 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 <[email protected]>
1 parent 9867436 commit 5c9cb78

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,14 @@ public function compileEndOnce()
306306
}
307307

308308
/**
309-
* Compile a selected block into valid PHP.
309+
* Compile a boolean value into a raw true / false value for embedding into HTML attributes or JavaScript.
310310
*
311-
* @param string $condition
311+
* @param bool $condition
312312
* @return string
313313
*/
314-
protected function compileSelected($condition)
314+
protected function compileBool($condition)
315315
{
316-
return "<?php if{$condition}: echo 'selected'; endif; ?>";
316+
return "<?php echo ($condition ? 'true' : 'false'); ?>";
317317
}
318318

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

363+
/**
364+
* Compile a selected block into valid PHP.
365+
*
366+
* @param string $condition
367+
* @return string
368+
*/
369+
protected function compileSelected($condition)
370+
{
371+
return "<?php if{$condition}: echo 'selected'; endif; ?>";
372+
}
373+
363374
/**
364375
* Compile the push statements into valid PHP.
365376
*

tests/View/Blade/BladeBoolTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\View\Blade;
4+
5+
class BladeBoolTest extends AbstractBladeTestCase
6+
{
7+
public function testBool()
8+
{
9+
// For Javascript object{'isBool' : true}
10+
$string = "{'isBool' : @bool(true)}";
11+
$expected = "{'isBool' : <?php echo ((true) ? 'true' : 'false'); ?>}";
12+
$this->assertEquals($expected, $this->compiler->compileString($string));
13+
14+
// For Javascript object{'isBool' : false}
15+
$string = "{'isBool' : @bool(false)}";
16+
$expected = "{'isBool' : <?php echo ((false) ? 'true' : 'false'); ?>}";
17+
$this->assertEquals($expected, $this->compiler->compileString($string));
18+
19+
// For Alpine.js x-show attribute
20+
$string = "<input type='text' x-show='@bool(true)' />";
21+
$expected = "<input type='text' x-show='<?php echo ((true) ? 'true' : 'false'); ?>' />";
22+
$this->assertEquals($expected, $this->compiler->compileString($string));
23+
24+
// For Alpine.js x-show attribute
25+
$string = "<input type='text' x-show='@bool(false)' />";
26+
$expected = "<input type='text' x-show='<?php echo ((false) ? 'true' : 'false'); ?>' />";
27+
$this->assertEquals($expected, $this->compiler->compileString($string));
28+
}
29+
30+
public function testCompileBool(): void
31+
{
32+
$someViewVarTruthy = 123;
33+
$compiled = $this->compiler->compileString('@bool($someViewVarTruthy)');
34+
35+
ob_start();
36+
eval(substr($compiled, 6, -3));
37+
$this->assertEquals('true', ob_get_clean());
38+
39+
$someViewVarFalsey = '0';
40+
$compiled = $this->compiler->compileString('@bool($someViewVarFalsey)');
41+
42+
ob_start();
43+
eval(substr($compiled, 6, -3));
44+
$this->assertEquals('false', ob_get_clean());
45+
46+
$anotherSomeViewVarTruthy = new SomeClass();
47+
$compiled = $this->compiler->compileString('@bool($anotherSomeViewVarTruthy)');
48+
49+
ob_start();
50+
eval(substr($compiled, 6, -3));
51+
$this->assertEquals('true', ob_get_clean());
52+
53+
$anotherSomeViewVarFalsey = null;
54+
$compiled = $this->compiler->compileString('@bool($anotherSomeViewVarFalsey)');
55+
56+
ob_start();
57+
eval(substr($compiled, 6, -3));
58+
$this->assertEquals('false', ob_get_clean());
59+
}
60+
}
61+
62+
class SomeClass
63+
{
64+
public function someMethod()
65+
{
66+
}
67+
}

0 commit comments

Comments
 (0)