Ricky Payne
Ricky Payne

Reputation: 81

How to load a module with Ajax Joomla

I am working on a joomla template where html/php pages are loaded dynamically into a content div via menu items using the following jquery function.

$page.load($lnkLoc);

The link loads a separate page that reads something like this.

    <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>

But, although everything works well, the joomla modules do not display, and instead the following is listed in the html

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

The only thing that does load is the footer module because it was present on the initial page load. What am I doing wrong? Am I attempting the impossible?

Upvotes: 0

Views: 1391

Answers (1)

FlavioEscobar
FlavioEscobar

Reputation: 877

Your separate page should be a PHP file that requires some Joomla files before rendering the module. The code below should work:

<?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);
?>

A simple jQuery load instruction like this is able to load the file that renders the module: $('#module_container').load('loadmodule.php');

Upvotes: 1

Related Questions