|
|
Home Modules Module Hello World 1 |
| Module Hello World 1 |
|
|
|
IntroductionThis tutorial aims to give you a grounding in the basic concepts for writing Joomla! modules. It will develop a very simple Hello World module and then extend it using patTemplate, a powerful set of functions that help separate the presentation (HTML) from the application logic (PHP) using templates. Full documentation for patTemplate can be found on their website.RequirementsYou need for this tutorial:
Let's RollWe will be creating two files in this tutorial in the folder called/modules/. Let's look at the files we need.
Installing the Basic ModuleYou cannot create a module from scratch from the Joomla! Administrator, so we have to make some basic files first and then install them as a module.Let's make the actual module first. Save the following code as mod_helloworld.php.<?php /** * @version 1.0 $ * @package HelloWorld * @copyright (C) 2005 Andrew Eddie * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL */ /** ensure this file is being included by a parent file */ defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); ?> <h1>Hello World</h1>Next save the following code in a file named mod_helloworld.xml.
<?xml version="1.0" encoding="iso-8859-1"?> <mosinstall type="module" version="4.5.2"> <name>Hello World</name> <author>Andrew Eddie</author> <creationDate>February 2005</creationDate> <copyright>(C) 2005 Andrew Eddie</copyright> <license>http://www.gnu.org/copyleft/gpl.html GNU/GPL</license> <version>1.0</version> <description>A module that says hello</description> <files> <filename module="mod_helloworld">mod_helloworld.php</filename> </files> <params /> </mosinstall>The first line of the file is a definition statement. You do not have to worry to much about what it means, but it must be in the file and it must be the first line (there cannot be any spaces before it). The other tags mean:
mod_helloworld.zip or you can download a copy here (mod_helloworld.zip).Follow these instructions to install the basic module:
If all goes well, you should now see a message indicating a successful install. Click on the Continue link.While you are still logged into the Joomla! Administrator, select Modules -> Site Modules from the Menu. Scroll down until you see the listing for Hello World. You will see that the module is unpublished and assigned to the left position in the template. Click on the red X in the published column to publish the module. Preview your site. You should see your module saying hi to you.Congratulations, you have built and deployed your first module. Now that it is installed we can modify the files directly to add more features. Improving the PresentationYou will find that you new module has been installed in the/modules directory of your site. First we will separate the presentation layer from the module file.Create a new directory under /modules called /mod_helloworld. In this new directory create a file called default.html. Copy the following code in this file:<mos:comment>
@version 4.5.2
@package HelloWorld
@copyright (C) 2005 Andrew Eddie
@license http://www.gnu.org/copyleft/gpl.html GNU/GPL
</mos:comment>
<mos:tmpl name="helloworld">
<h1>Hello World</h1>
The time is {TIME}
</mos:tmpl>
We have added a little extra code here to demonstrate how to add a variable to your module template. You will also notice that the HTML is wrapped in a mos:tmpl tag. This defines a template and we have given the template the name of helloworld.Now, find mod_helloworld.php in the /modules directory and open it in your editor.Tip: There are many quality editors that are available for free, PSPad and HTML-Kit to name a few. For something a little more powerful, you might like to try out Eclipse.
Delete the existing code and replace it with the following: <?php /** * @version 1.0 * @package HelloWorld * @copyright (C) 2005 Andrew Eddie * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL */ /** ensure this file is being included by a parent file */ defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' ); // load the patTemplate library require_once( $mosConfig_absolute_path . '/includes/patTemplate/patTemplate.php' ); // create the template $tmpl =& patFactory::createTemplate( '', false, false ); // set the path to look for html files $tmpl->setRoot( dirname( __FILE__ ) . '/mod_helloworld' ); $tmpl->readTemplatesFromInput( 'default.html' ); $tmpl->addVar( 'helloworld', 'time', date( 'H:i:s' ) ); $tmpl->displayParsedTemplate( 'helloworld' ); ?>Let's examine what is happening in this file.
Save all your files and refresh your browser. You should see that the module now displays the time. You can download the files for the final part of the tutorial here (mod_helloworld_1.zip). Note that the XML file has been updated to include the new HTML file. To suggest a change to this page please click here. |
|
| Last Updated ( Wednesday, 11 July 2007 ) |
| < Prev | Next > |
|---|






If all goes well, you should now see a message indicating a successful install. Click on the
Preview your site. You should see your module saying hi to you.