API15

JComponentHelper/renderComponent: Difference between revisions

From Joomla! Documentation

Doxiki (talk | contribs)
No edit summary
m preparing for archive only
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
<span class="editsection" style="font-size:76%;">
<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[Description:JComponentHelper/renderComponent|Edit Descripton]]<nowiki>]</nowiki>
<nowiki>[<! removed edit link to red link >]</nowiki>
</span>
</span>


{{Description:JComponentHelper/renderComponent}}
<! removed transcluded page call, red link never existed >


===Syntax===
===Syntax===
Line 126: Line 125:


<span class="editsection" style="font-size:76%;">
<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[SeeAlso:JComponentHelper/renderComponent|Edit See Also]]<nowiki>]</nowiki>
<nowiki>[<! removed edit link to red link >]</nowiki>
</span>
</span>
{{SeeAlso:JComponentHelper/renderComponent}}
<! removed transcluded page call, red link never existed >


===Examples===
===Examples===
<CodeExamplesForm />
=== Code Examples ===
<dpl>
<dpl>
  noresultsheader=\n
  noresultsheader=\n
  category=renderComponent
  category=renderComponent
  category=JComponentHelper
  category=JComponentHelper
  category=CodeExample
  namespace=CodeExample
  category=MethodExample
  category=MethodExample
  include=*
  include=*
  format= ,,,
  format= ,,,
</dpl>
</dpl>
[[Category:Archived pages API15]]

Latest revision as of 00:24, 25 March 2017

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax

renderComponent($name=null, $params=array())
Parameter Name Default Value Description
$name null
$params array()

Defined in

libraries/joomla/application/component/helper.php

Importing

jimport( 'joomla.application.component.helper' );

Source Body

function renderComponent($name = null, $params = array())
{
        global $mainframe, $option;

        if(empty($name)) {
                // Throw 404 if no component
                JError::raiseError(404, JText::_("Component Not Found"));
                return;
        }

        $scope = $mainframe->scope; //record the scope
        $mainframe->scope = $name;  //set scope to component name

        // Build the component path
        $name = preg_replace('/[^A-Z0-9_\.-]/i', '', $name);
        $file = substr( $name, 4 );

        // Define component path
        define( 'JPATH_COMPONENT',                                      JPATH_BASE.DS.'components'.DS.$name);
        define( 'JPATH_COMPONENT_SITE',                         JPATH_SITE.DS.'components'.DS.$name);
        define( 'JPATH_COMPONENT_ADMINISTRATOR',        JPATH_ADMINISTRATOR.DS.'components'.DS.$name);

        // get component path
        if ( $mainframe->isAdmin() && file_exists(JPATH_COMPONENT.DS.'admin.'.$file.'.php') ) {
                $path = JPATH_COMPONENT.DS.'admin.'.$file.'.php';
        } else {
                $path = JPATH_COMPONENT.DS.$file.'.php';
        }

        // If component disabled throw error
        if (!JComponentHelper::isEnabled( $name ) || !file_exists($path)) {
                JError::raiseError( 404, JText::_( 'Component Not Found' ) );
        }

        // Handle legacy globals if enabled
        if ($mainframe->getCfg('legacy'))
        {
                // Include legacy globals
                global $my, $database, $id, $acl, $task;

                // For backwards compatibility extract the config vars as globals
                $registry =& JFactory::getConfig();
                foreach (get_object_vars($registry->toObject()) as $k => $v)
                {
                        $varname = 'mosConfig_'.$k;
                        $$varname = $v;
                }
                $contentConfig = &JComponentHelper::getParams( 'com_content' );
                foreach (get_object_vars($contentConfig->toObject()) as $k => $v)
                {
                        $varname = 'mosConfig_'.$k;
                        $$varname = $v;
                }
                $usersConfig = &JComponentHelper::getParams( 'com_users' );
                foreach (get_object_vars($usersConfig->toObject()) as $k => $v)
                {
                        $varname = 'mosConfig_'.$k;
                        $$varname = $v;
                }

        }

        $task = JRequest::getString( 'task' );

        // Load common language files
        $lang =& JFactory::getLanguage();
        $lang->load($name);

        // Handle template preview outlining
        $contents = null;

        // Execute the component
        ob_start();
        require_once $path;
        $contents = ob_get_contents();
        ob_end_clean();

        // Build the component toolbar
        jimport( 'joomla.application.helper' );
        if (($path = JApplicationHelper::getPath( 'toolbar' )) && $mainframe->isAdmin()) {

                // Get the task again, in case it has changed
                $task = JRequest::getString( 'task' );

                // Make the toolbar
                include_once( $path );
        }

        $mainframe->scope = $scope; //revert the scope

        return $contents;
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

Code Examples