简体   繁体   中英

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 .'"' : ''}}>

我亲爱的朋友,您只需要更改代码

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

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 }}">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM