Joomla 2.5渲染com_content组件输出

时间:2013-01-02 09:29:12

标签: joomla joomla2.5

是否可以从外部脚本呈现Joomla内容?例如,我有一些html字符串,我想传递给com_content组件,以使所有内容插件和模块功能可用。我想我应该使用JDocumentRendererComponent课程。我的外部文件中的代码:

<?php

require_once ('framework.php'); //loading joomla framework

jimport('joomla.document.html.renderer.component');

$contentHtml = '<p>Some content html</p>';

echo JDocumentRendererComponent::render('com_content',array(),$contentHtml);

?>

我得到的是最后一行的错误:

Fatal error: Class 'JDocumentRendererComponent' not found...

我做错了什么?有任何想法吗?

2 个答案:

答案 0 :(得分:0)

这是因为您没有将Joomla框架包含在外部脚本中。使用以下代码。这将确保Joomla!环境正确加载

/* Initialize Joomla framework */
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );
define( 'DS', DIRECTORY_SEPARATOR );
/* Required Files */
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
/* To use Joomla's Database Class */
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
/**************************************************/
// Your code starts here...
// Remember that the Site application isn't running, so you cannot access $mainframe or any of its methods.
/**************************************************/

JDocumentRendererComponent类位于/libraries/joomla/document/html/renderer/component.php中,如果你正确加载框架,一切都应该正常工作。

答案 1 :(得分:0)

我为我的问题找到了其他解决方案。该工作也可以通过内容插件事件(触发器)完成。来自components/com_content/views/article/view.html.php的代码:

JPluginHelper::importPlugin('content');
$results = $dispatcher->trigger('onContentPrepare', array ('com_content.article', &$item, &$this->params, $offset));

$item->event = new stdClass();
$results = $dispatcher->trigger('onContentAfterTitle', array('com_content.article', &$item, &$this->params, $offset));
$item->event->afterDisplayTitle = trim(implode("\n", $results));

$results = $dispatcher->trigger('onContentBeforeDisplay', array('com_content.article', &$item, &$this->params, $offset));
$item->event->beforeDisplayContent = trim(implode("\n", $results));

$results = $dispatcher->trigger('onContentAfterDisplay', array('com_content.article', &$item, &$this->params, $offset));
$item->event->afterDisplayContent = trim(implode("\n", $results));

所以我们实际上可以从字符串中创建一个对象并将其传递给这些触发器。因此,我们获得的内容就像文章一样,具有主要功能。

有关它的更多信息:

http://www.inmotionhosting.com/support/edu/joomla-25/create-plugin/content-plugin-events https://groups.google.com/forum/#!msg/joomla-dev-cms/VZVurjiZWIs/9Vr45KS2LTMJ