laravel 是否可以在 HTML 中为刀片模板编写内联 IF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43996772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to write an IF inline in HTML for a blade template
提问by Gabbax0r
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.
我的刀片模板中有一个复选框元素,我想知道是否可以在 html 元素内编写 if 语句。
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?
但是在输入标签内,代码会产生一堆错误。有什么特别需要知道的,或者我必须像我的第一个例子那样做吗?
回答by Sagar Jajoriya
<input placeholder="" name="holiday" id="holiday" {{ ($data->holiday) ? "checked" : "" }} type="checkbox" value="1">
回答by mohamed ali
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>
回答by Akshay Patil
You can try something like this
你可以试试这样的
<input type="checkbox" {{ ($icv->ic_to_unit_fk == $uv->unit_pkey)? 'checked="true"' : '' }}>