Jared
Jared

Reputation: 3

Passing a parameter in a Joomla! module

I am creating a simple module in joomla. I have a file mod_progress.php.

defined( '_JEXEC' ) or die( 'Restricted access' );
// Include the syndicate functions only once
require_once( dirname(__FILE__).'/helper.php' );
require( JModuleHelper::getLayoutPath( 'mod_progress' ) );

$document = JFactory::getDocument();
$document->addStyleSheet(JURI::base() . 'modules/mod_progress/tmpl/styles.css');

$percentValues = htmlspecialchars($params->get('percentValues'));

The last line is what of interest here. I want to take the variable $percentValues and pass it on for use in the module's default.php template.

In my default.php all I have is:

<?php echo $percentValues; ?> 

This does not work. The error I get tells me the variable is undefined.

However, if I do:

<?php $percentValues = htmlspecialchars($params->get('percentValues'));
 echo $percentValues; ?>

It works just fine. Can someone explain why I can't use the variable?There must be something big I am missing. Using Joomla! 3.1.

Thank you in advance.

Jared

Upvotes: 0

Views: 3074

Answers (1)

rynhe
rynhe

Reputation: 2529

Rearrange your code

defined( '_JEXEC' ) or die( 'Restricted access' );
// Include the syndicate functions only once
require_once( dirname(__FILE__).'/helper.php' );

$percentValues = htmlspecialchars($params->get('percentValues'));

$document = JFactory::getDocument();
$document->addStyleSheet(JURI::base() . 'modules/mod_progress/tmpl/styles.css');

require( JModuleHelper::getLayoutPath( 'mod_progress' ) );

Should declare the variable before include the layout.

Upvotes: 2

Related Questions