Store PHP variable with HTML in JavaScript in a Blade Laravel template

I need a way to store HTML from a PHP variable in JavaScript.

var contents = "{{ $template }}";

However, this generates an error. I think this is because it did not slip away properly, so it confuses the web browser because JS cannot save it properly ... I tried Laravel escaping

var contents = "{{ e($template) }}";

without any success.

Final goal: $('#preview-iframe').contents().find('html').html(contents);

How can i do this?

+4
source share
5 answers

Double curly braces {{ }}will always convert special characters to HTML objects. Try using {!! !!}to accurately display the code. However, you need to make sure that you avoid double quotes in the string.

, :

var contents = "{{ $template }}";

- :

var contents = "{!! addcslashes($template, '"') !!}";
+8

var contents = atob("{{ base64_encode($contents) }}");

, , , javascript .

, atob() IE10 -

+1

PHP java script

<script>
   var jsvariable = <? echo $phpvarialbe ?>;
   console.log(jsvariable)
</script>
+1

var contents = {{$template->toJSON()}};

0

. ,

var contents = "{{ e($template) }}";

javascript, , {{e ($ template)}} - javascript, . script , JSON.

, ,

var contents = '@include("public.default.values.content")';

( views)/public/default/values/content.blade.php ,

{{ e($template) }}

Include blade- javascript. , javascript-, -, javascript.

-1
source

Source: https://habr.com/ru/post/1543036/


All Articles