Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I know that there are other question addressing this issue but they mostly cover adding a value to an attribute based on a variable. I want to include several attributes and their values based on a condition.

My first attempt, as you can see below, besides not working is not very readable:

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

/>

Produces the 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 />
like image 667
Diego Alves Avatar asked Jul 06 '17 15:07

Diego Alves


1 Answers

Change your blade syntax to {!! !!}

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

By default, Blade escape Html - See the documentation

like image 57
OuailB Avatar answered Oct 21 '22 18:10

OuailB