user1040259
user1040259

Reputation: 6509

Laravel blade simple inline if statement

Is it possible with Laravel Blade templating to write a statement like this?

If alt image field exists in database, add. If not, leave out. What is happening with the code below is the double quotes alt="text" get rendered.

<img src="#" {{$product->extra_img1_alt ? 'alt="'. $product->extra_img1_alt .'"' : ''}}>

Upvotes: 0

Views: 6131

Answers (2)

ceejayoz
ceejayoz

Reputation: 180004

Text in {{ tags are escaped automatically, so you need something more like this:

<img src="#"
     @if($product->extra_img1_alt)
         alt="{{ $product->extra_img1_alt }}"
     @endif
>

That said, while you seem to be trying to avoid it, there's nothing really wrong with an empty alt attribute:

<img src="#" alt="{{ $product->extra_img1_alt }}">

Upvotes: 2

Mahdi Bashirpour
Mahdi Bashirpour

Reputation: 18803

My dear friend, you just need to change the code

<img src="#" alt=" @if($product->extra_img1_alt) {{$product->extra_img1_alt}} @endif ">

Upvotes: 3

Related Questions