If the module doesn't return anything, pass the return array instead

Now you can set variables in the module itself and have them returned to the template instead of needing to explicitly return the data. Will come in handy in scenarios where you've extended another module and want to retain it's return data. Previously you had to set a variable and then add to that and return it from the child class.
This commit is contained in:
Josh Sherman 2012-10-14 18:08:27 -04:00
parent 3c2c936cee
commit 8fb7622061
3 changed files with 56 additions and 6 deletions

View file

@ -368,7 +368,17 @@ class Controller extends Object
* module know to use the cache, either passing in a variable * module know to use the cache, either passing in a variable
* or setting it on the object * or setting it on the object
*/ */
$display->setModuleReturn($valid_request && $valid_security_hash ? $module->__default() : array('status' => 'error', 'message' => $error_message)); if ($valid_request && $valid_security_hash)
{
$module_return = $module->__default();
if ($module_return === null)
{
$module_return = $module->return;
}
}
$display->setModuleReturn(isset($module_return) ? $module_return : array('status' => 'error', 'message' => $error_message));
unset($error_message); unset($error_message);

View file

@ -158,6 +158,19 @@ class Module extends Object
*/ */
protected $template = 'index'; protected $template = 'index';
/**
* Return
*
* Array that is returned to the template in the case of the module not
* returning anything itself. This is somewhat of a one way trip as you
* cannot get the variable unless you reference the return array explicitly
* $this->return['variable']
*
* @access protected
* @var array
*/
protected $return = array();
/** /**
* Constructor * Constructor
* *
@ -198,14 +211,16 @@ class Module extends Object
/** /**
* Magic Setter Method * Magic Setter Method
* *
* Prohibits the direct modification of module variables. * Places the variables that are being modified in the return array that is
* returned if nothing is returned by the module itself. This also prohibits
* the direct modification of module variables which could cause issues.
* *
* @param string $name name of the variable to be set * @param string $name name of the variable to be set
* @param mixed $value value of the variable to be set * @param mixed $value value of the variable to be set
*/ */
public function __set($name, $value) public function __set($name, $value)
{ {
throw new Exception('Cannot set module variables directly'); $this->return[$name] = $value;
} }
/** /**

31
jar.php
View file

@ -1315,7 +1315,17 @@ class Controller extends Object
* module know to use the cache, either passing in a variable * module know to use the cache, either passing in a variable
* or setting it on the object * or setting it on the object
*/ */
$display->setModuleReturn($valid_request && $valid_security_hash ? $module->__default() : array('status' => 'error', 'message' => $error_message)); if ($valid_request && $valid_security_hash)
{
$module_return = $module->__default();
if ($module_return === null)
{
$module_return = $module->return;
}
}
$display->setModuleReturn(isset($module_return) ? $module_return : array('status' => 'error', 'message' => $error_message));
unset($error_message); unset($error_message);
@ -5552,6 +5562,19 @@ class Module extends Object
*/ */
protected $template = 'index'; protected $template = 'index';
/**
* Return
*
* Array that is returned to the template in the case of the module not
* returning anything itself. This is somewhat of a one way trip as you
* cannot get the variable unless you reference the return array explicitly
* $this->return['variable']
*
* @access protected
* @var array
*/
protected $return = array();
/** /**
* Constructor * Constructor
* *
@ -5592,14 +5615,16 @@ class Module extends Object
/** /**
* Magic Setter Method * Magic Setter Method
* *
* Prohibits the direct modification of module variables. * Places the variables that are being modified in the return array that is
* returned if nothing is returned by the module itself. This also prohibits
* the direct modification of module variables which could cause issues.
* *
* @param string $name name of the variable to be set * @param string $name name of the variable to be set
* @param mixed $value value of the variable to be set * @param mixed $value value of the variable to be set
*/ */
public function __set($name, $value) public function __set($name, $value)
{ {
throw new Exception('Cannot set module variables directly'); $this->return[$name] = $value;
} }
/** /**