使用 Laravel 中的刀片模板在 php 标签内打印 JavaScript 变量值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20177054/
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 08:41:30  来源:igfitidea点击:

Print JavaScript variable value inside a php tag using blade template in Laravel

jqueryajaxlaravellaravel-4blade

提问by Sagiruddin Mondal

Suppose this is a JS code and I want to print the value of id. Here id is a variable with a value receiving into the jq function.

假设这是一个 JS 代码,我想打印id. 这里 id 是一个变量,其值接收到 jq 函数中。

function follow_or_unfollow(id,action){         
     myUrl = "{{ action('FollowsController@show', 'id') }}" ;               
}

If we mension 'id'it will show the id string.

如果我们提到'id'它,它将显示 id 字符串。

PS. here {{ is using as it meant as php code in blade template.

附注。这里 {{ 使用,因为它意味着刀片模板中的 php 代码。

Now i need to print the script variable inside a php code.

现在我需要在 php 代码中打印脚本变量。

回答by Aniket

I am afraid that what you are asking is not possible. The simple reason being JavaScript is client-side and PHP is server-side.

恐怕你问的是不可能的。原因很简单,JavaScript 是客户端,而 PHP 是服务器端。

You can always place PHP code inside JavaScript because it will just be echoed there and won't run at that very instant. Whereas if you want to use a JavaScript variable inside PHP, that means you are trying to access a variable which doesn't even exist at the point of when the server processes the request.

您始终可以将 PHP 代码放在 JavaScript 中,因为它只会在那里回显并且不会立即运行。而如果您想在 PHP 中使用 JavaScript 变量,则意味着您正在尝试访问一个在服务器处理请求时甚至不存在的变量。

Update

更新

There is a workaround to this. You can pass the data which is needed in JS as parameters in the URL and then get these values in PHP using $_GET.

有一个解决方法。您可以将 JS 中需要的数据作为 URL 中的参数传递,然后在 PHP 中使用$_GET.

回答by shamanSK

Probably its late, but I think I have solution. Your code

可能已经晚了,但我想我有解决方案。你的代码

myUrl = "{{ action('FollowsController@show', 'id') }}" ;

is inside javascript block. Therefore the blade parser use parenthesis as javascript object rather as blade variable. So you can't use any PHP variables inside script block. In my solution I would replace your line with:

在 javascript 块内。因此,刀片解析器使用括号作为 javascript 对象而不是刀片变量。所以你不能在脚本块中使用任何 PHP 变量。在我的解决方案中,我会将您的线路替换为:

myUrl = "@include("public.default.js_values.myUrl")" ;

and then create that template file

然后创建该模板文件

(in your views folder)/public/default/js_values/myUrl.blade.php

and add one line into that file

并在该文件中添加一行

{{ action('FollowsController@show', 'id') }}

This way blade parser will include the content on myUrl.blade.php. In file will correctly resolve that in parenthesis is variable and returns the value of php variable (in your case the method) back to javascript.

这样刀片解析器将包含 myUrl.blade.php 上的内容。在文件中将正确解析括号中的变量并将php变量的值(在您的情况下为方法)返回给javascript。

I have tested this in similar situation and it worked.

我在类似的情况下对此进行了测试,并且有效。

回答by user1709694

Just use {!! $vars !!}.

只需使用 {!! $vars !!}。

var link = {!! "'".url('string/'.$vars.'/string')."'" !!};