根据 Laravel 5.3 中的条件在 blade 模板中插入 html 属性的好方法

Good way to insert html atributes in blade template based on conditions in Laravel 5.3

我知道还有其他问题可以解决这个问题,但它们主要涉及向基于变量的属性添加值。我想根据条件包含几个属性及其值。

我的第一次尝试,正如你在下面看到的,除了不工作之外可读性不是很好:

<input
type="text" 
{{ $errors->has('bedrooms') ? "data-toggle='tooltip' data-placement='right' 
title='please, enter a value between 1 and 9.'" : "" }}

/>

产生 html:

<input type="text"
data-toggle=&quot;tooltip&quot; data-placement=&quot;right&quot; 
title=&quot;please, enter a value between 1 and 9.&quot />

将 blade 语法更改为 {!! !!}

<input type="text" 
  {!! $errors->has('bedrooms') ? "data-toggle='tooltip' data-placement='right' title='please, enter a value between 1 and 9.'" : "" !!}
/>

默认情况下,Blade转义Html-See the documentation