将PHP变量传递给JavaScript

将PHP变量传递给JavaScript

本文介绍了在Yii中,将PHP变量传递给JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的JavaScript中(使用jQuery),我需要访问一整套PHP变量。虽然我只是将JavaScript文件作为视图生成,然后使用 renderPartial()来回显主视图中的JavaScript。

In my JavaScript (using jQuery) there are a whole set of PHP variables to which I need access. While I've got it working by just producing the JavaScript file as a view, and then using renderPartial() to echo the JavaScript inside the main view.

然而,这显然不是很优雅,所以我想知道'Yii'的做法。我一直在看资产管理器,但这似乎是静态JavaScript文件 - 你不能在那里包含PHP(除非我错了)。

However, this is obviously not very elegant, so I would like to know the 'Yii' way of doing it. I've been looking at the Assets Manager but that seems to be for static JavaScript files - you can't include PHP in there (unless I'm wrong).

还有另一种方法吗?

推荐答案

您可以考虑。在我看来,它更好,因为有一个名为$ position的参数,它可以帮助你控制render()的进程输出。

You may consider registerScript. In my opinion, it is better since there is a param named $position, which can help you control the process output of render().

这篇关于在Yii中,将PHP变量传递给JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 10:15