Search code examples
phphtmlphpstormlaravel-blade

PhpStorm thinks <div> is open but it isn't with blade directives


Here is the code:

@if (count($errors) > 0)
    <div class="errorClass">
@else
    <div class="cleanClass">
@endif
<p>Some stuff</p>
</div>

The above code make PhpStorm register that there is an open <div>. Any way to fix this bug?

This is making it frustrating to use when it comes time to refactor code.

I installed the Blade extension hoping that would help - it didn't. PhpStorm reports:

Element div is not closed.

Any way to make PhpStorm smarter?


Solution

  • (Posted on behalf of the OP).

    Workaround

    This works but is not easy to read.

    <div
        @if (count($errors) > 0)
             class="errorClass"
        @else
             class="cleanClass"
        @endif
    >
        <p>Here is a paragraph</p>
    </div>
    

    Best Situation for readability

    A simple ternary would do the job better.

    //Open as normal
    <div class="{{ count($errors) > 0 ? 'errorClass' : 'cleanClass' }}">
        <p>This is some text</p>
    </div>