如何使用Ajax Joomla加载模块

时间:2015-07-10 17:13:54

标签: ajax joomla

我正在开发一个joomla模板,其中使用以下jquery函数通过菜单项将html / php页面动态加载到内容div中。

$page.load($lnkLoc);

该链接会加载一个单独的页面,内容如下所示。

    <div class="contentarea">
<jdoc:include type="modules" name="page2cont1" title="Page two top" />
</div>
<div class="contentarea">
<jdoc:include type="modules" name="page2cont2" title="Page two middle" />
</div>
<div class="contentarea">
<jdoc:include type="modules" name="page2cont3" title="Page two bottom" />
</div>
<footer>
<jdoc:include type="modules" name="footer" title="Footer" />
</footer>

但是,虽然一切运作良好,但joomla模块不会显示,而是在html中列出以下内容

<div class="contentarea"><jdoc:include type="modules" name="page2cont1" title="Page two top"></jdoc:include>

唯一能加载的是页脚模块,因为它出现在初始页面加载中。我究竟做错了什么?我在尝试不可能吗?

1 个答案:

答案 0 :(得分:1)

您的单独页面应该是一个PHP文件,在渲染模块之前需要一些Joomla文件。 下面的代码应该有效:

<?php
// Name of this file: loadmodule.php

define( '_JEXEC', 1 ); // This constant is required by all Joomla files.

// The JPATH_BASE should be defined as the path to the root Joomla installation.
define( 'JPATH_BASE', '<path to joomla root directory>' );
define( 'DS', DIRECTORY_SEPARATOR );

// Those requires below will allow us to use Joomla's instructions.
require_once ( JPATH_BASE .DS. 'includes' .DS. 'defines.php' );
require_once ( JPATH_BASE .DS. 'includes' .DS. 'framework.php' );

// Instantiate Joomla to be able to use it.
$mainframe =& JFactory::getApplication('site');

// Loading and rendering the module.
$modules = JModuleHelper::getModules('testloadmodule');
foreach( $modules as $module ) echo JModuleHelper::renderModule($module);
?>

像这样的简单jQuery加载指令能够加载呈现模块的文件:$('#module_container').load('loadmodule.php');