在 Yii 中,将 PHP 变量传递给 JavaScript [英] In Yii, pass PHP variables to 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).
还有其他方法吗?
推荐答案
你可以考虑 registerScript.在我看来,最好有一个名为 $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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!