Home Tutorials Passing parameters

Passing parameters

  previous next
February 08, 2009 by Victor    

OK, we have managed to display object properties and use class methods, hmm what’s next? Well, you ‘ve seen that you can configure parameters for most modules at the backend, this is really nice stuff since you can let your module suit different needs or change its layout. This is the next trouble free kingdom to conquer.

So, say we want to create a module for the administrator to greet users and guests. She should be able to define two greeting phrases, one for guests and one for registered users. Module parameters are defined within the XML file under the params tag. Goes like this:

<?xml version="1.0" encoding="utf-8"?>
<install type="module" version="1.5.0">
        <name>blank</name>
        <author>me</author>
        <version>0.0.1</version>
        <description>TEST</description>
        <files>
                <filename module="mod_blank">mod_blank.php</filename>
        </files>
        <params>
                <param name="saluteguest" type="text"
                   default="Hello stranger"
                   label="gsalute"
                   description="Salute text for guests" />
                <param name="saluteregistered" type="text" 
                   default="Welcome "
                   label="rsalute"
                   description="Salute text for registered users" />
        </params>
</install>

We just declared two text boxes (single line), what you get is:

example parameters for blank Joomla module

Now, I bet you need to know how these parameters are passed to our main code:

<?php
  defined('_JEXEC') or die('Restricted access');
 
  $user =& JFactory::getUser();
 
  # Get backend parameters
  $saluteg = $params->get("saluteguest");
  $saluter = $params->get("saluteregistered");
 
  if ($user->guest)
  {
      echo "<p>$saluteg</p>";
  }
  else
  {
      echo "<p>$saluter</p>";
  }
?>

Add comment


Security code
Refresh