Actions

User

Difference between revisions of "Rvsjoen/tutorial/Developing a Module/Part 02"

From Joomla! Documentation

< User:Rvsjoen‎ | tutorial/Developing a Module
(Adding a module template)
m (Testing your component)
 
Line 100: Line 100:
 
* <tt>[[#tmpl/default.php|tmpl/default.php]]</tt>
 
* <tt>[[#tmpl/default.php|tmpl/default.php]]</tt>
  
== Testing your component ==
+
== Testing your module ==
  
 
For details on how to install the module into your Joomla! site, refer to the information provided in
 
For details on how to install the module into your Joomla! site, refer to the information provided in

Latest revision as of 17:46, 13 March 2012

Contents

Adding a module template

In most cases, we want our module to have the ability to display output using a template. If we wanted to we could forget all about the template and write the code directly into the main PHP file, however, this is a hard-coded and not very flexible approach. Instead, by properly separating business logic and view logic we are able to develop much more flexible extensions, where the output template of the module could be overridden in templates and alternative styles could be provided for the module.

So in order to create a module template we need to do two things

  1. Create the template
  2. Tell the module to load the template and display it

With your favorite editor, create the following file

tmpl/default.php

<?php
 
/**
 * @package     Joomla.Tutorials
 * @subpackage  Module
 * @copyright   Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license     License GNU General Public License version 2 or later; see LICENSE.txt
 */
 
// No direct access to this file
defined('_JEXEC') or die;
 
?>
 
<p>Hello World! I am a module</p>

This is pretty straight forward, all we are really doing is creating a php file that outputs a paragraph, nothing more, nothing less.

Next thing on our list is to modify the module php file to load this awesome template.

With your favorite editor, edit the following file

mod_helloworld.php

<?php
 
/**
 * @package     Joomla.Tutorials
 * @subpackage  Module
 * @copyright   Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license     License GNU General Public License version 2 or later; see LICENSE.txt
 */
 
// No direct access to this file
defined('_JEXEC') or die;
 
require JModuleHelper::getLayoutPath('mod_helloworld', $params->get('layout', 'default'));

As you can see, the only thing that is required for this module to use our template is a call to JModuleHelper::getLayoutPath(). The first argument is the name of the module to find the template for (this is naturally our very own mod_helloworld module). The second argument is a bit harder to grasp, but it gives the module manager the option to assign an alternative layout to the module. The layout parameter is a parameter which is available to all module in the module manager. You could just use 'default' as the second argument to ignore alternative layouts.

Lastly we need to modify the XML manifest so our new folder is copied into Joomla! when the module is installed.

mod_helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="2.5.0" method="upgrade">
 
        <name>Hello World!</name>
        <!-- The following elements are optional and free of formatting constraints -->
        <creationDate>Once upon a time</creationDate>
        <author>John Doe</author>
        <authorEmail>john.doe@example.org</authorEmail>
        <authorUrl>http://www.example.org</authorUrl>
        <copyright>Copyright Info</copyright>
        <license>License Info</license>
        <!--  The version string is stored in the extension table -->
        <version>0.0.2</version>
        <!-- The description is optional and defaults to the name -->
        <description>Description of the Hello World module ...</description>
 
        <!-- Note the folder attribute: This attribute describes what to copy
                into the module folder -->
        <files>
                <filename module="mod_helloworld">mod_helloworld.php</filename>
                <filename>mod_helloworld.xml</filename>
                <folder>tmpl</folder>        </files>
 
</extension>

As you can see by the highlighted line in the XML manifest, we just have to list the folder and everything inside it will be automatically copied into the module folder when the module is installed.

File listing

Testing your module

For details on how to install the module into your Joomla! site, refer to the information provided in Part 01.

Download this part

Download example package

Articles in this series

This tutorial is supported by the following versions of Joomla!

Joomla 2.5