public static function module($Name, $Properties = array())
{
if (isset($Properties['cache'])) {
$Key = isset($Properties['cachekey']) ? $Properties['cachekey'] : 'module.' . $Name;
$Result = Gdn::cache()->get($Key);
if ($Result !== Gdn_Cache::CACHEOP_FAILURE) {
// Trace('Module: '.$Result, $Key);
return $Result;
}
}
try {
if (!class_exists($Name)) {
if (debug()) {
$Result = "Error: {$Name} doesn't exist";
} else {
$Result = "<!-- Error: {$Name} doesn't exist -->";
}
} else {
$Module = new $Name(Gdn::controller(), '');
$Module->Visible = true;
// Add properties passed in from the controller.
$ControllerProperties = Gdn::controller()->data('_properties.' . strtolower($Name), array());
$Properties = array_merge($ControllerProperties, $Properties);
foreach ($Properties as $Name => $value) {
// Check for a setter method
if (method_exists($Module, $method = 'set' . ucfirst($Name))) {
$Module->{$method}($value);
} else {
$Module->{$Name} = $value;
}
}
$Result = $Module->toString();
}
} catch (Exception $Ex) {
if (debug()) {
$Result = '<pre class="Exception">' . htmlspecialchars($Ex->getMessage() . "\n" . $Ex->getTraceAsString()) . '</pre>';
} else {
$Result = $Ex->getMessage();
}
}
if (isset($Key)) {
// Trace($Result, "Store $Key");
Gdn::cache()->store($Key, $Result, array(Gdn_Cache::FEATURE_EXPIRY => $Properties['cache']));
}
return $Result;
}