在 Laravel Blade 模板中的 JavaScript 中使用 HTML 存储 PHP 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24011238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:36:01  来源:igfitidea点击:

Store PHP variable with HTML in JavaScript in Laravel Blade Template

javascriptphpjquerylaravel

提问by John

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

我需要一种在 JavaScript 中从 PHP 变量存储 HTML 的方法。

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

This generates an error however. I guess it's because it's not escaped properly, So it messes up the web browser, because JS cannot store it properly... I've tried Laravel escaping

但是,这会产生错误。我猜是因为它没有正确转义,所以它弄乱了网络浏览器,因为 JS 无法正确存储它......我试过 Laravel 转义

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

without any success.

没有任何成功。

The end goal is: $('#preview-iframe').contents().find('html').html(contents);

最终目标是: $('#preview-iframe').contents().find('html').html(contents);

How can I accomplish this?

我怎样才能做到这一点?

回答by jon__o

The double curly brackets {{ }}will always convert special characters to HTML entities. Try using {!! !!}to render the code exactly. However, you will want to make sure you escape the double quotes in the string.

双大括号{{ }}将始终将特殊字符转换为 HTML 实体。尝试使用{!! !!}来准确地呈现代码。但是,您需要确保对字符串中的双引号进行转义。

So this:

所以这:

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

Should be something like:

应该是这样的:

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

回答by Mirza Brunjadze

I used this workaround for the same problem

我用这个解决方法解决了同样的问题

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

This is very convenient, because you don't have to escape any characters, and you are guaranteed that there won't be any syntax errors with javascript.

这非常方便,因为您不必转义任何字符,并且您可以保证使用 javascript 不会出现任何语法错误。

One downside is that atob() function is supported in IE10 and up - Source

一个缺点是 IE10 及更高版本支持 atob() 函数 - 来源

回答by bujji

You can take a PHP variable value to java script as bellow

您可以将 PHP 变量值带到 Java 脚本中,如下所示

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

回答by boortmans

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

你可以试试 var contents = {{$template->toJSON()}};

回答by shamanSK

I came across very similar problem. The problem is that if you use

我遇到了非常相似的问题。问题是,如果你使用

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

inside your javascript, the parser thinks {{ e($template) }} is the javascript object and not your blade variable. Parentheses inside script blockare not treated as blade variable, but as object in JSON format.

在您的 javascript 中,解析器认为 {{ e($template) }} 是 javascript 对象,而不是您的刀片变量。脚本块内的括号不被视为刀片变量,而是作为 JSON 格式的对象。

My solution was, that I made a template and included it into place of blade variable like this

我的解决方案是,我制作了一个模板并将其包含在这样的刀片变量中

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

Then I created file in folder (your views folder)/public/default/values/content.blade.phpand added into this template file only one line, your value

然后我在文件夹(您的视图文件夹)/public/default/values/content.blade.php 中创建了文件并仅添加到此模板文件中的一行,您的值

{{ e($template) }}

Include is correctly understood by blade parser inside javascript block. This way the blade parser will correctly use your include in javascript code, correctly go to content blade, get the value and save it in you javascript variable.

javascript 块内的刀片解析器正确理解包含。这样,刀片解析器将正确使用您在 javascript 代码中的包含,正确转到内容刀片,获取值并将其保存在您的 javascript 变量中。