Laravel Blade without extra spaces?

If you do this, you will receive an error message:

<p>@if($foo)@if($bar)test@endif@endif</p> 

And if you do this, you will get <p> test </p> by adding too much white:

 <p>@if($foo) @if($bar)test@endif @endif</p> 

Is there any way to avoid this?

Edit: see my answer below for an updated answer. There is a clean way to do this without hacks or external libraries.

+8
php laravel blade
source share
4 answers

it looks like a lot of search traffic is coming out, so I decided that I would add an update to share how I handle it these days. This is essentially a bit more code, but in the end it becomes silly 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 are working with a blade, do not try to squeeze a lot of conventions into the elements. Rather, the result of the conditional contains an element. It is clean, easy to read, and contains only a few characters.

+3
source share

Try with the ternary operator, there is no space in Laravel

 <p>{{ $foo ? ($bar ? 'test' : '') : ''}}</p> 
+21
source share

You can always use the hedronium / spaceless-blade package in the package to add this feature to Blade.

+7
source share

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 a suggestion for a new tag

+3
source share

All Articles