0

I'm facing a problem when trying to inject a string from PHP (using the Laravel framework) into javascript.

The string is like [{"foo1": bar1}, {"foo2" : bar2}]. When I inject this string to javascript using something like var test = '{{ $var }}' the " are converted to ". The javascript string is used in a function later to parse the JSON. This now fails because of the quote-conversion.

It seems that the string is passed into the browser as it should be. But Chrome converts it.

How can I solve this? I've tried PHP's str_replace(), html_entity_decode(), json_encode() and Javascript's replace(). Both aren't solving the issue.

3
  • Are you using a template engine ? Commented Feb 27, 2015 at 10:05
  • could you not just pass in valid json to begin with? Commented Feb 27, 2015 at 10:08
  • You shouldn't get the behaviour you describe unless you have something transforming the variable before it gets inserted. The {{ }} syntax suggests some kind of template engine. You'll need to provide more information about what that is. Commented Feb 27, 2015 at 10:09

1 Answer 1

1

I think you are using blade template in laravel. Could you please try

{ $var } instead of {{ $var }} 
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! Since I'm using Laravel5 it had to be {!! $var !!}. Thanks, +1 and accepted. Have a nice day :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.