Modules are lightweight and flexible extensions that are displayed in pages at different module positions. These modules are displayed like boxes arranged around a component in top, sidebar or footer position. For example, login module or html module.
Each module has a single entry point located in the modules directory:
This file has three functions. It includes helper file, call helper functions to get required data, and finally includes the template that will display the module output.
Let's create a simple Joomla module that will display Hello World.
There are four basic files in module:
mod_helloworld.php: This file is the entry point for the module. It will perform any necessary initialization routines, call helper routines to collect any necessary data, and include the template which will display the module output.
mod_helloworld.xml: This file contains information about the module. It defines the files that need to be installed by the Joomla! installer and specifies configuration parameters for the module.
helper.php: This file contains the helper class which is used to do the actual work in retrieving the information to be displayed in the module, from the database or some other source.
tmpl/default.php: This is the module template. This file will take the data collected by mod_helloworld.php and generate the HTML to be displayed on the page.
This file will perform three tasks:
A simple line to include helper.php file using require_once statement. This file is used to do all database queries and prepare the data for the view.
require_once dirname(__FILE__) . '/helper.php';
require_once is used because helper functions are defined within a class, and we only want the class defined once.
Then, you need to load the data from the helper and put it into an array. Helper class will contain one method: getHello(). The helper class method is invoked in the following way:
$hello = modHelloWorldHelper::getHello($params);
The last line of code tells the module to load the default layout (tmpl/default.php) for the view. Finally, template file is included in the following way:
This file contains the helper class that is used to retrieve the data to be displayed in the module output. In this example, helper class will have one method: getHello(). This method will return the ‘Hello, World’ message.
public static function getHello($params)
return 'Hello, World!';
In Joomla, the content is separated from the presentation, so you need to create a view file to display the content. The view should not contain any database queries, you only want to use the view to present the content. This allows a user to potentially create a template override of this view and modify the output without having any clue about how the rest of the module works.
This file is the template which displays the module output. This can be done in following way:
The template file has the same scope as the mod_helloworld.php file. It means that the variable $hello can be defined in the mod_helloworld.php file and then used in the template file without any extra declarations or function calls.