Reputation: 75
what is the similar blade syntax for the following code?
<?php if(...):?>abc<?php else:?>cde<?php endif;?>
the code @if(...) abc @else .... is no good as it ads a space before and after the "abc" the code @if(...)abc@endif (no spacing before and after html string) generates error ...
Thanks
Upvotes: 1
Views: 2959
Reputation: 17
I have found the solution in case there is space issue between @if()@endif and @if. I have replaced the @if() @endif with the {{$wardname}} variable to be printed using {{$wardname}}@if and its removed conflict with @endif & @if
and applied logic like:
if($city->wardname!="") {
$wardname = "(".$city->wardname.")";
}else{
$wardname = "";
}
Implemented it as: {{$wardname}}@if
Upvotes: 0
Reputation: 158
It's a workaround, but I still think we always should remember that it's a PHP environment... So, simply:
@php
if(...) echo "abc";
else echo "cde";
@endphp
Upvotes: 0
Reputation: 4196
Solution
The correct solution for this problem would be following:
@if(1==1){{ '1' }}@endif
This happens often and makes problem with "space sensitive" parts of code like values in <option>
tags.
Having the @if(1==1) 1 @endif
would be compiled to following which shows empty spaces around the number:
<?php if(1==1): ?> 1 <?php endif; ?>
Solution code would be compiled to this code:
<?php if(1==1): ?><?php echo e('1'); ?><?php endif; ?>
Which pretty much explains why this won't make additional spaces.
Upvotes: 6
Reputation: 1072
I've run into similar problems with spaces.
A workaround I use is this:
Put your if statement into php tags
@php
$myVar = 'abc';
if(...) $myVar = 'cde';
@endphp
and then echo the defined variable
{{$myVar}}
Upvotes: 1
Reputation: 3912
The correct syntax is:
@if(...)
abc
@else
cde
@endif
I never saw unwanted spaces with this one.
Upvotes: -2
Reputation: 75
Did a bit more research and it looks like there is no solution as there is no spaceless tag in blade. I found a solution from someone who wrapping his string in extra hml tags (so that is easy as he ads spaces before and after the tag and the string inside tag si spaceless) but in my case I just need a string no spaces before and after ... I will go the old fashion php way ...
Upvotes: 1