Laravel Blade without extra whitespace?

前端 未结 5 1165
难免孤独
难免孤独 2021-01-04 03:23

If you do this you get an error:

@if($foo)@if($bar)test@endif@endif

And if you do this, you get

test

相关标签:
5条回答
  • 2021-01-04 04:03

    Try with a ternary operator, there is no whitespace control in Laravel

    <p>{{ $foo ? ($bar ? 'test' : '') : ''}}</p>
    
    0 讨论(0)
  • 2021-01-04 04:07

    this appears to be getting a lot of search traffic so I figured I'd add an update to share how I'm handling this these days. Basically, it's a little more code but it ends up being stupid simple and very clean:

    @if($foo)
      <p>Test</p>
    @elseif($bar)
      <p>Test2</p>
    @else
      <p>Test3</p>
    @endif
    

    The moral of the story is when you're working with blade, don't try to cram a lot of conditionals within elements. Rather, have the result of the conditional contain the element. It's clean, easy to read, and with only a few more characters spent.

    0 讨论(0)
  • 2021-01-04 04:08

    You could always use the hedronium/spaceless-blade package on packagist to add this functionality to Blade.

    0 讨论(0)
  • 2021-01-04 04:22

    As far as I know there is no spaceless tag in Blade. If you want to use standard Blade tags you will have extra spaces. There is a github discussion with proposal for new tag

    0 讨论(0)
  • 2021-01-04 04:23

    You can add {{""}} in between the code you want to close or connect without space.

    <p>@if($foo)@if($bar)test@endif{{""}}@endif</p>
    
    0 讨论(0)
提交回复
热议问题