【问题标题】:Joomla 2.5 render com_content component outputJoomla 2.5 渲染 com_content 组件输出
【发布时间】:2012-12-16 16:10:30
【问题描述】:

是否可以从外部脚本呈现 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...

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

【问题讨论】:

  • 这就是你所做的一切?您没有实例化应用程序?
  • @Elin,我确实实例化了应用程序。它在这里:require_once('framework.php')。我不提供此文件内容,但初始化效果很好。

标签: joomla joomla2.5


【解决方案1】:

这是因为您没有将 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 如果您正确加载框架,一切都应该正常工作。

【讨论】:

  • 我在这里加载 Joomla 环境:require_once('framework.php')。我不提供此文件内容,但无论如何 Joomla 框架已完美加载。
  • 我的应用程序初始化和你的差不多。在同一个脚本中(未粘贴此示例中的代码)我正在加载 Joomla db 方法,例如:JFactory::getDbo() 等。它可以工作。所以我假设框架加载正确。
  • 您运行的是什么版本的平台? JDocumentRendererComponent 在当前平台中不存在,但在 11.3 中确实存在。
  • @Elin:是的,函数是正确的,我可以在我的 IDE 中导航到它(CTRL + MouseClick)
【解决方案2】:

我为我的问题找到了其他解决方案。这项工作也可以通过内容插件事件(触发器)来完成。来自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

【讨论】:

    猜你喜欢
    • 2013-11-22
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    • 2012-12-24
    • 2012-11-24
    • 2016-03-08
    相关资源
    最近更新 更多