First Joomla 1.5 module: Displaying contents from another webpage

For my first Joomla 1.5 module, I had to write a module that would get the contents from another webpage (actually, just a number) and display it in the module area.

To make it more challenging, I also added the following requirements:

  • Has to use caching so as to not hit the webpage each time.
  • Should allow the “template” for displaying the content to be editable by the webmaster

This is the code I wrote – simple, but its my first Joomla 1.5 module.

mod_webnumber.php

<?php
//no direct access
defined('_JEXEC' )
or die('Direct Access to this location is not allowed.');

// Get a reference to the global cache object.
$cache = & JFactory::getCache();
$cache->setCaching(1); // make sure it caches

$number  = $cache->call( array( 'Website', 'latestNumber' ) );

// get a parameter from the module's configuration
$template = $params->get('template');
$output = str_replace ("#n", $number, $template);

echo $output;

class Website {
    function latestNumber() {
        // Get the latest number from the webpage
        $number = file_get_contents (
          "http://www.yourwebsite.com/");
        $number = strip_tags ($number);

        return $number;
    }
}
?>

mod_webnumber.xml (mostly derived from the official Joomla docs site on creating a module, following is only the parameters I used which the module depends on)

    <files>
        <!-- The "module" attribute signifies that this is the main controller file -->
        <filename module="mod_webnumber">mod_webnumber.php</filename>
    </files>
<params>
        <param name="moduleclass_sfx" type="text" default="" label="Module Class Suffix" description="PARAMMODULECLASSSUFFIX" />
        <param name="@spacer" type="spacer" default="" label="" description="" />
        <param name="template" type="text" default="#n is the number you need." label="Template" description="TEMPLATE" />
    </params>

No comments yet

Leave a reply