在 joomla 中将 php 变量传递给 javascript

passing php variables to javascript in joomla

我正在尝试为 joomla 制作一个模块,该模块使用 google 地图制作地图 API。

如果数据硬编码在 javascript 文件中,地图本身工作正常,但我希望能够在多个站点上轻松使用它,因此最好从 joomla 后端设置所有选项. 我一直在尝试通过 JSON 调用来执行此操作,但它 returns 在页面加载时出现错误 500。 我的开发站点是:http://dev.xander.dk.web1.symatic.dk/

相关代码如下:

mod_google_maps.php(创建变量并将它们连接到 mod_google_maps.xml 中的字段)

<?php
/**

 * Hello World! Module Entry Point
 * 
 * @package    Joomla.Tutorials
 * @subpackage Modules
 * @link http://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module
 * @license        GNU/GPL, see LICENSE.php
 * mod_helloworld is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */

// no direct access
defined('_JEXEC') or die;
// Include the syndicate functions only once
require_once( dirname(__FILE__) . '/helper.php' );

$mapCenterLat = $params->get('mapCenterLat', '55.395239');
$mapCenterLng = $params->get('mapCenterLng', '10.380180');
$zoom = $params->get('zoom', '18');
$mapDisableUi = $params->get('mapDisableUi', false);
$markerLat = $params->get('markerLat', '55.395239');
$markerLng = $params->get('markerLng', '10.380180');
$markerTitle = $params->get('markerTitle', 'symatic');

helper.php

<?php
function getMapOptionsAjax(){
    $data = array('mapCenterLat' => $mapCenterLat, 'mapCenterLng' => $mapCenterLng, 'zoom' => $zoom, 'mapDisableUi' => $mapDisableUi, 'markerLat' => $markerLat, 'markerLng' => $markerLng, 'markerTitle' => $markerTitle );
    echo json_encode($data);
};
?>

maps.js(仅相关 JSON 调用)

jQuery.get('index.php?option=com_ajax&module=google_maps&method=getMapOptions&format=json', function(data){
    var mapResponse = data;
    console.log(mapResponse);
});

我省略了 maps.js 文件的其余部分,因为它与这个问题无关,而且它适用于硬编码值。

500 error 是因为您正在尝试访问 getMapOptionsAjax() 中的未定义变量。

我相信你的想法是错误的。

如果您只想设置通过 Joomla 的模块管理器在后端定义的值,那么这些值实际上是静态值(即您的 $params)。所以你应该在你的 modules 模板文件中设置它们,即 /modules/mod_google_maps/tmpl/default.php

在该文件中,您可以添加一个部分来设置一些 Javascript 变量供您的 maps.js 文件使用。例如

<script>
    var mapCenterLat = <?php echo $params->get('mapCenterLat', '55.395239'); ?>;
    var mapCenterLng = <?php echo $params->get('mapCenterLng', '10.380180'); ?>;
    var zoom = <?php echo $params->get('zoom', '18'); ?>;
    var mapDisableUi = <?php echo $params->get('mapDisableUi', false); ?>;
    var markerLat = <?php echo $params->get('markerLat', '55.395239'); ?>;
    var markerLng = <?php echo $params->get('markerLng', '10.380180'); ?>;
    var markerTitle = '<?php echo $params->get('markerTitle', 'symatic'); ?>';
</script>

它将呈现并像这样发送到浏览器:

<script>
    var mapCenterLat = 55.395239;
    var mapCenterLng = 10.380180;
    var zoom = 18;
    var mapDisableUi = false;
    var markerLat = 55.395239;
    var markerLng = 10.380180;
    var markerTitle = 'symatic'; // Note the quotes added around the echo
</script>

注意mapDisableUi将return它在mod_google_maps.xml中分配的值

注意markerTitle[的字符串值在echo两边加上单引号' =22=]