【问题标题】:Joomla plugin get contents of whole page before outputJoomla插件在输出前获取整个页面的内容
【发布时间】:2015-08-08 09:31:56
【问题描述】:

使用我的 Joomla 插件,我想在创建输出之前获取和修改所有内容。

function newContent($html)
    {
    $html = "new content";
    return $html;
    }

function onContentPrepare()
    {
    ob_start(array($this, "newContent"));
    return true;
    }

function onContentBeforeDisplay()
    {
    ob_end_flush();
    return true;
    }

我尝试使用onContentAfterDisplay,但它继续只改变一小部分而不是所有输出。

【问题讨论】:

    标签: php joomla output


    【解决方案1】:

    你为什么不这样做:

    public function onContentPrepare($context, &$article, &$params, $page = 0)
    {
         $article->text = "new content";
    }
    

    ?

    编辑

    根据您的回复,这是一种在插件中修改整个页面内容/正文的方法。将此方法添加到您的插件中:

    public function onAfterRender()
    {
        $app = JFactory::getApplication();
        $currentBodyToChange = $app->getBody();
    
        //do something with $currentBodyToChange
        //$bodyChanged is modified $currentBodyToChange
    
        $app->setBody($bodyChanged);
    }
    

    【讨论】:

    • 您好,我想获取和修改所有内容(head、body、footer),所有页面,不仅仅是文章
    • @arbogast 如果您在页面上有单篇文章,但如果您有另一个组件或什至“精选文章” onAfterRender 不会触发,则此操作很顺利。你知道是否有办法在任何页面上做到这一点?我注意到的另一件奇怪的事情 - 在管理方面它每次都会触发。
    【解决方案2】:

    要停止在管理页面上触发插件...

    $app = JFactory::getApplication();
    if ($app->isSite())  echo 'Front end - do it!';
    if ($app->isAdmin()) echo 'Admin pages - ignore!';
    

    【讨论】:

      猜你喜欢
      • 2010-12-07
      • 2011-12-11
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 2011-07-11
      • 2012-02-27
      相关资源
      最近更新 更多