Joomla 3 在外部文件中加载模板


Joomla 3 load template in external file

我创建了一个外部文件。我想在该文件中加载 joomla 默认模板。我使用了下面的代码,它导致了一个错误:

致命错误:调用受保护的方法 JApplicationSite::render() from 上下文 ' '

define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', realpath(dirname(__FILE__) ));
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
$mainframe = JFactory::getApplication('site');
JPluginHelper::importPlugin('system');
$mainframe->initialise();
$myContent = "Hello World!!";
$document = JFactory::getDocument();
$document->setBuffer($myContent, 'component');
$mainframe->render();
echo $mainframe;

至少你需要让Joomla应用程序知道你想渲染哪个模板。

//normal external script initialisation
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath('/home/somepath/public_html/somedirectory/'));
define( 'DS', DIRECTORY_SEPARATOR ); 
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php');
require_once( JPATH_LIBRARIES .DS.'joomla'.DS.'factory.php');
$app = JFactory::getApplication('site');
$app->initialise();
//jimport( 'joomla.document.html.html' ); required in certain cases
//initialise the doc to use the Html renderer
$doc = new JDocumentHtml();
//minimal info required
$options['directory'] = "/my/template/directory";
$options["template"] = $app->getTemplate();//get the template name
$options["file"] = "index.php";//usually
//Optional: Manually set params
//$params = new JRegistry();
//$params->colorVariation = "blue";
//$params->widthStyle = "fluid";
//$params->showComponent = "1";
//$options["params"] = $params;
//you may initialise the document object here and set the buffers
//$document = JFactory::getDocument();
//$document->setBuffer($contents, 'module', $name);
// $document->setHeadData($data['head']);etc
echo $doc->render(false, $options);
$app->close();

当然,如果您使用相对 url,请不要指望它的显示方式相同,尤其是。可用于其他目的,但如检查文档模板中的脚本。在Joomla 3.3 PHP 5.5上测试。它不适用于 1.5 等较低版本。