User

User:Rvsjoen/tutorial/Developing a Module/Part 02

From Joomla! Documentation

Adding a module template

In most cases, we want our module to have the ability to display output using a template. We could theoretically code all of this directly into the module php file but using a separate file increases flexibility and gives us the option to override the module output in a template.

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>

Then we modify the module php file to load this awesome template.

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.