-1

I use Php7.0 and Laravel 5.0 and want to initiate a var in javascript on blade page.

But html_entity_decode function can not decode the special chars.

userName =  "{{ html_entity_decode($user->name) }}"
console.log(userName);

I would like to see " ' < > but it gives me &quot; &#039; &lt; &gt;

While checkin in db I can see correctly.

Any ideas?

5
  • stackoverflow.com/questions/35030977/…, laravel.com/docs/5.1/blade#displaying-data Commented Mar 26, 2019 at 9:57
  • Isn't it {!! html_entity_decode($user->name) !!}?? Commented Mar 26, 2019 at 9:58
  • Doesn't laravel decode strings between braces automatically as of Laravel 5+? Commented Mar 26, 2019 at 9:58
  • I tried to {!! but javascript gives error. Because I want to assign username to javascript var. Commented Mar 26, 2019 at 9:59
  • 1
    Please get out of the habit of giving useless statements like “but javascript gives error”. Quote error messages verbatim, and also show the code that causes them (in this case the client-side code that whatever you put into your template produced, as well.) Commented Mar 26, 2019 at 10:08

1 Answer 1

2

I did some quick testing. And the following example works on my side;

<script type="text/javascript">
    var userName = {!! json_encode($user->name) !!};
    console.log(userName);
</script>

When I check my console, the userName is displayed properly.

Sign up to request clarification or add additional context in comments.

Comments

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.