Skip to content

Conversation

@david-valdivia
Copy link
Contributor

Add @Bool directive functionality to Blade, allowing boolean values to be printed directly into strings or used in object construction.

Examples:

JS

<script>
    let config = {
        isActive: @bool($isActive),
        hasAccess: @bool($hasAccess)
    };
</script>

Alpine

<div x-data="{ isActive: @bool($isActive) }">
    <button :class="{ 'active': isActive }">Toggle</button>
</div>

Bootstrap:

<div class="dropdown">
  <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="@bool($hasPopup)" aria-expanded="@bool($isExpanded)">
    Dropdown button
  </button>
  <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
  </div>
</div>

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


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.

@taylorotwell taylorotwell merged commit 5c9cb78 into laravel:11.x Oct 16, 2024
31 checks passed
@MrPunyapal
Copy link
Contributor

btw it works with @js(true) out of the box.

@ahinkle
Copy link
Contributor

ahinkle commented Oct 29, 2024

@david-valdivia What's the difference between @js($condition) and @bool($condition)? Would you care to contribute to the docs?

@david-valdivia
Copy link
Contributor Author

@MrPunyapal @ahinkle

@david-valdivia What's the difference between @js($condition) and @bool($condition)? Would you care to contribute to the docs?

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.

Cases:
Captura de pantalla 2024-10-29 a la(s) 10 33 54 a m

Results:
Captura de pantalla 2024-10-29 a la(s) 10 34 55 a m

@Bool() always returns a boolean value, @js() converts into a json.

@MrPunyapal
Copy link
Contributor

He means PR on laravel/docs

@david-valdivia
Copy link
Contributor Author

He means PR on laravel/docs

haha, i`m sorry, of course.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants