John
John

Reputation: 197

Summernote set text through passed parameter in laravel

My laravel controller passes parameters to the view which I can access by $note in {{ }}. All right, it works. I can get note's title. However, when I want to set the HTML for summernote, it somehow doesn't work. This is what I tried:

<script type="text/javascript">
$('#editor').summernote('code', '{{ $note->html }}');

as well as unescaped using {!! $note->html !!}}. The result is a summernote editor which looks like a normal text input. However, when manually setting [...]('code', '<html><h1>some html for example</h1></html>)[...] it works like a charm.

It is obvious that it's related to the way blade is putting out data using {{ }} but I can't currently figure out why. Using unescaped html had been the first approach that popped up in my mind.

Upvotes: 0

Views: 1664

Answers (1)

ceejayoz
ceejayoz

Reputation: 180105

When outputting PHP data for JavaScript to use, I like to use json_encode on the variable. This guarantees variables get output in a JavaScript-friendly version - it'll wrap strings in ' marks, leave integers alone, and output complex arrays/objects as proper JSON.

$('#editor').summernote('code', {!! json_encode($note->html) !!});

Upvotes: 2

Related Questions