Laravel Blade without extra whitespace?
Asked Answered
W

6

20

If you do this you get an error:

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

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

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

Is there a way to avoid this?

Whet answered 1/10, 2014 at 16:27 Comment(0)
W
10

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.

Whet answered 21/4, 2017 at 4:40 Comment(3)
I had this problem with my breadcrumbs, I wrapped them up in a <span>. Basically the same solution. Thank you for your answer!Hynes
While it pains me to resort to this approach (as there appears to be no alternative), it is surprisingly effective. Thank you!Eulaeulachon
@user151496 care to explain why?Whet
B
46

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

<p>{{ $foo ? ($bar ? 'test' : '') : ''}}</p>
Bootee answered 1/10, 2014 at 16:30 Comment(1)
Upvoted for alternate solution for the specific example.Whet
O
14

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

<p>@if($foo)@if($bar)test@endif{{""}}@endif</p>
Overabundance answered 18/5, 2020 at 9:36 Comment(0)
G
10

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

Glomma answered 20/4, 2015 at 13:1 Comment(2)
aint working for foreach loop, still getting the spacesCasting
The referenced package is designed to eliminate spaces in content between already-parsed Blade directives, not between adjacent directives with only whitespace between them. The link in Macin's Answer clarifies the distinction. This is the reason for which Symfony's Twig engine includes {% spaceless %} and {% endspaceless %}, despite the fact that its tag syntax employs opening and closing delimiters ({% and %}). In other words, the suggested solution addresses a different problem.Eulaeulachon
W
10

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.

Whet answered 21/4, 2017 at 4:40 Comment(3)
I had this problem with my breadcrumbs, I wrapped them up in a <span>. Basically the same solution. Thank you for your answer!Hynes
While it pains me to resort to this approach (as there appears to be no alternative), it is surprisingly effective. Thank you!Eulaeulachon
@user151496 care to explain why?Whet
P
4

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

Politico answered 1/10, 2014 at 16:31 Comment(0)
P
0

value="@error('title'){{old('title')}}@else{{''}}@isset($myBook->title){{$myBook->title}}@endisset{{''}}@enderror">

this works for me.

Pearce answered 17/4 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.