Back to ComponentHelper class

Method renderComponent

public static string
renderComponent
(mixed $option, mixed $params = array())
Render the component.
Parameters
  • string $option The component option.
  • array $params The component parameters
Returns
  • string
Since
  • 1.5
-
  • \Joomla\CMS\Component\Exception\MissingComponentException

Method renderComponent - Source code

/**
 * Render the component.
 *
 * @param   string  $option  The component option.
 * @param   array   $params  The component parameters
 *
 * @return  string
 *
 * @since   1.5
 * @throws  MissingComponentException
 */
public static function renderComponent($option, $params = array())
{
    $app = Factory::getApplication();
    $lang = Factory::getLanguage();
    if (!$app->isClient('api')) {
        // Load template language files.
        $template = $app->getTemplate(true)->template;
        $lang->load('tpl_' . $template, JPATH_BASE) || $lang->load('tpl_' . $template, JPATH_THEMES . "/{$template}");
    }
    if (empty($option)) {
        throw new MissingComponentException(Text::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND'), 404);
    }
    if (JDEBUG) {
        Profiler::getInstance('Application')->mark('beforeRenderComponent ' . $option);
    }
    // Record the scope
    $scope = $app->scope;
    // Set scope to component name
    $app->scope = $option;
    // Build the component path.
    $option = preg_replace('/[^A-Z0-9_\\.-]/i', '', $option);
    // Define component path.
    if (!\defined('JPATH_COMPONENT')) {
        /**
         * Defines the path to the active component for the request
         *
         * Note this constant is application aware and is different for each application (site/admin).
         *
         * @var    string
         * @since  1.5
         * @deprecated 5.0 without replacement
         */
        \define('JPATH_COMPONENT', JPATH_BASE . '/components/' . $option);
    }
    if (!\defined('JPATH_COMPONENT_SITE')) {
        /**
         * Defines the path to the site element of the active component for the request
         *
         * @var    string
         * @since  1.5
         * @deprecated 5.0 without replacement
         */
        \define('JPATH_COMPONENT_SITE', JPATH_SITE . '/components/' . $option);
    }
    if (!\defined('JPATH_COMPONENT_ADMINISTRATOR')) {
        /**
         * Defines the path to the admin element of the active component for the request
         *
         * @var    string
         * @since  1.5
         * @deprecated 5.0 without replacement
         */
        \define('JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR . '/components/' . $option);
    }
    // If component is disabled throw error
    if (!static::isEnabled($option)) {
        throw new MissingComponentException(Text::_('JLIB_APPLICATION_ERROR_COMPONENT_NOT_FOUND'), 404);
    }
    ob_start();
    $app->bootComponent($option)->getDispatcher($app)->dispatch();
    $contents = ob_get_clean();
    // Revert the scope
    $app->scope = $scope;
    if (JDEBUG) {
        Profiler::getInstance('Application')->mark('afterRenderComponent ' . $option);
    }
    return $contents;
}