php_beginner php_beginner - 3 months ago 6
PHP Question

Convert PHP variable into Javascript Variable with escaping character

I've been trying to convert a php variable into a java script variable. At the same time I want to save it in a format with HTML tags. The problem is I'm Unable to escape the '<' this character in code.

var lecturers = [
@foreach ($Lecturers as $L)
'{{ '<option>'.$L->Name.'</option>' }}',
@endforeach
];


The Output I received through console log is as follows

["&lt;option&gt;&lt;/option&gt;", "&lt;option&gt;&lt;/option&gt;", "&lt;option&gt;&lt;/option&gt;"]


How can i get the output as

"<option> Name </option>"


I'm using laravel as Framework.

Answer

You have to use {!! !!} instead to avoid escaping:

var lecturers = [
     @foreach ($Lecturers as $L)
       '{!! '<option>'.$L->Name.'</option>' !!}',
     @endforeach
 ];
Comments