vendredi 31 mars 2017

Printing json in Laravel templates

I'm having trouble in moving Eloquent data to Laravel templates as JSON. The problematic data is user inputted text which has linebreaks, all kinds of special characters, quotes, hyphens, MS Word xml garbage tags, you name it.

When I print it to the template as

<script>App.data = JSON.parse('')</script>

I get all kinds of JSON parsing errors. Usually from linebreaks, but also from special characters and so on. I'm now getting rid of them by ugly hacks, like

$data->body = str_replace("\n", "\\n", addslashes($data->body))

But there has to be a more elegant solution. Using the regular json_encode() leaves linebreaks etc in the JSON that end up as errors.

I'm using Laravel 5.3.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ooD4Mc
via IFTTT

Aucun commentaire:

Enregistrer un commentaire