Gdn_Theme::module PHP Method

module() public static method

public static module ( $Name, array $Properties = [] ) : mixed | string
$Name
$Properties array
return mixed | string
    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;
    }

Usage Example

Example #1
0
/**
 *
 *
 * @param array $Params
 * @param object $Smarty
 * @return string
 */
function smarty_function_module($Params, &$Smarty)
{
    $Name = val('name', $Params);
    unset($Params['name']);
    $Result = Gdn_Theme::module($Name, $Params);
    return $Result;
}
All Usage Examples Of Gdn_Theme::module