0

I have the following json

[{
  "date": "2011",
  "content": "<p>Hello world?</p>"
},
{
  "date": "2012",
  "content": "<p><strong>Hello again</strong></p>"
}]

my controller has

public function index() {
  $data['json'] = json_decode(file_get_contents('location_of_json_file.json'));
  return view('index', $data);
}

my view has

@foreach ($json as $a)
  {{ $a->content }}
@endforeach

but what i get is

&lt;p&gt;Hello world?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hello again&lt;/strong&gt;&lt;/p&gt;

how can i make it parse the html code instead of displaying the syntax? i've tried htmlentities and html_entity_decode. i tried to json_encode in different place of the code, i'm lost. please help.

2 Answers 2

1

In Laravel 5, by default {{ ... }} will escape the output using htmlentities. To output raw HTML that get's interpreted use {!! ... !!}:

@foreach ($json as $a)
    {!! $a->content !!}
@endforeach

Here's a comparison between the different echo brackets and how to change them

0
1

The Blade output tags changed between Laravel 4 and Laravel 5. You're looking for:

{!! $a->content !!}

In Laravel 4, {{ $data }} would echo data as is, whereas {{{ $data }}} would echo data after running it through htmlentities.

However, Laravel 5 has changed it so that {{ $data }} will echo data after running it through htmlentities, and the new syntax {!! $data !!} will echo data as is.

Documentation here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.