Is it possible to write an IF inline in HTML for a blade template

12,618

Solution 1

<input placeholder="" name="holiday" id="holiday" {{ ($data->holiday) ? "checked" : "" }} type="checkbox" value="1">

Solution 2

use as this code

<div class="input-field">
      <input placeholder="" name="holiday" id="holiday"

       {{ ($data->holiday)?'checked':'' }}

       //or

       @if($data->holiday) 'checked' @endif

       type="checkbox"
       value="1">
       <label for="holiday">Holiday</label>
</div>

Solution 3

You can try something like this

<input type="checkbox" {{ ($icv->ic_to_unit_fk == $uv->unit_pkey)? 'checked="true"' : '' }}>
Share:
12,618
Gabbax0r
Author by

Gabbax0r

Updated on July 13, 2022

Comments

  • Gabbax0r
    Gabbax0r almost 2 years

    I have a checkbox element in my blade template and I want to know if it is possible to write an if statement inside of a html element.

    This works:

    @if($data->holiday)
       <div class="input-field">
          <input placeholder="" name="holiday" id="holiday" checked
           type="checkbox"
           value="1">
           <label for="holiday">Holiday</label>
       </div>
    @else
       <div class="input-field">
           <input placeholder="" name="holiday" id="holiday" 
            type="checkbox"
            value="1">
            <label for="holiday">Holiday</label>
       </div>
    @endif
    

    Due to the doublecode I want to write something like this:

        <div class="input-field">
              <input placeholder="" name="holiday" id="holiday"
    
               {{if($data->holiday)?'checked':'' }}
    
               //or
    
               @if($data->holiday)?'checked':''@endif
    
               type="checkbox"
               value="1">
               <label for="holiday">Holiday</label>
        </div>
    

    But inside the input tag the code generates a bunch of errors. Is there something special to know or do I have to do it like in my first example?