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

时间:2014-12-23 14:26:27

标签: php joomla3.0

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

  

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

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;

1 个答案:

答案 0 :(得分:0)

至少你需要让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();

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