I am building a Joomla module which will eventually pull data from an external data source. Right now while I learn, I set it to simply print the string “This bit works correctly” to the module position. However, I have been having problems getting it to work correctly. Here is my code:
mod_ucr.php:
<?php
/**
* UniversalContentRepository Module Entry Point
*
* @package UniversalContentRepository
* @subpackage Modules
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
// Include the syndicate functions only once
require_once( dirname(__FILE__).DS.'helper.php' );
$helper = new modUCRHelper();
$content = $helper->getSnippetContent();
require( JModuleHelper::getLayoutPath( 'mod_ucr' ) );
?>
mod_ucr.xml
<?xml version="1.0" encoding="utf-8"?>
<install type="module" version="1.5.0">
<name>Universal Content Repository</name>
<author>Brendon Dugan</author>
<version>1.5.0</version>
<description>A module to allow the insertion of UCR Snippets into a Joomla site.</description>
<files>
<filename>mod_ucr.xml</filename>
<filename module="mod_ucr">mod_ucr.php</filename>
<filename>index.html</filename>
<filename>helper.php</filename>
<filename>tmpl/default.php</filename>
<filename>tmpl/index.html</filename>
</files>
<params>
</params>
</install>
helper.php:
<?php
class modUCRHelper
{
function __construct(){
}
public function getSnippetContent($id = 0){
$content = "This bit works correctly, ID = $id";
return $content;
}
}
?>
tmpl/default.php:
<?php // no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
echo $content;
?>
Here we set the variable $content to be the output of the getSnippetContent() method of the helper class. This method is currently:
function getSnippetContent($id = 0){
$output = "This bit works correctly";
return $output;
}
Which simply outputs the string I want to print. In my template, I should be able to echo content like this:
<?php // no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
echo $content;
?>
But what the module outputs instead is
"This bit works correctlyThis bit works correctly"
If I comment out the echo statement, the string is still echoed once, suggesting that the return statement itself is echoing.
I adapted my code from the “Hello World!” example located in the Joomla documentation. Any Ideas?
This is Core joomla code for displaying module
It uses the $content variable itself. And your module is changing the value of $content. One output is from your module and another is due to this joomla code. It appends the content of your module to $content variable (which has been changed in your module).
So do not use $content variable in you code.