Rvsjoen/tutorial/Developing a Module/Part 02: Difference between revisions
From Joomla! Documentation
Created page with "= 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 ..." |
|||
| Line 10: | Line 10: | ||
With your favorite editor, create the following file | With your favorite editor, create the following file | ||
<span id="tmpl/ | <span id="tmpl/default.php"> | ||
'''<tt>tmpl/ | '''<tt>tmpl/default.php</tt>''' | ||
<source lang="php"> | <source lang="php"> | ||
<?php | <?php | ||
| Line 30: | Line 30: | ||
</source> | </source> | ||
</span> | </span> | ||
Then we modify the module php file to load this awesome template. | |||
<span id="mod_helloworld.php"> | |||
'''<tt>mod_helloworld.php</tt>''' | |||
<source lang="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')); | |||
</source> | |||
</span> | |||
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. | |||
Revision as of 15:27, 13 March 2012
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
- Create the template
- 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.