I have a JSON file with text. I want to print that out into a div and have an anchor tag inside it.
I have the JSON –
JavaScript
x
{
"text" : "A bunch of text here with a <a href="#">link</a>"
}
Inside my controller, I decode the JSON
JavaScript
$json = Storage::disk('local')->get('settings.json');
$json = json_decode($json, true);
return $json;
Inside my view
JavaScript
<p>{{ $text }}</p>
At the moment the p tag would just echo the response as a string
A bunch of text here with a <a href=”#”>link</a>
How can turn this into html? Is that even a thing?
Advertisement
Answer
Basically Laravel does html entity encoding when you print something using {{ $text }}
.
In order to not to encode HTML elements, you have to use
JavaScript
{!! $text !!}
This can be also used for web page rendering as well for many other use case.