Skip to content
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

Merged
merged 7 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,17 @@ protected function compileSelected($condition)
return "<?php if{$condition}: echo 'selected'; endif; ?>";
}

/**
* Compile a bool block into valid PHP.
*
* @param bool $condition
* @return string
*/
protected function compileBool($condition)
{
return "<?php if{$condition}: echo 'true'; else: 'false'; endif; ?>";
Copy link
Contributor

@bert-w bert-w Oct 16, 2024

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'; ?>";

}

/**
* Compile a checked block into valid PHP.
*
Expand Down
30 changes: 30 additions & 0 deletions tests/View/Blade/BladeBoolTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Illuminate\Tests\View\Blade;

class BladeBoolTest extends AbstractBladeTestCase
{
public function testBool()
Copy link
Contributor

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());

    }

Copy link
Contributor Author

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.

{

// For Javascript object{'isBool' : true}
$string = "{'isBool' : @bool(true)}";
$expected = "{'isBool' : <?php if(true): echo 'true'; else: 'false'; endif; ?>}";
$this->assertEquals($expected, $this->compiler->compileString($string));

// For Javascript object{'isBool' : false}
$string = "{'isBool' : @bool(false)}";
$expected = "{'isBool' : <?php if(false): echo 'true'; else: 'false'; endif; ?>}";
$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 if(true): echo 'true'; else: 'false'; endif; ?>' />";
$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 if(false): echo 'true'; else: 'false'; endif; ?>' />";
$this->assertEquals($expected, $this->compiler->compileString($string));
}
}