1

I'm trying to understand appendBody

First of all append would seem like it comes after the </body> but I assume it can only mean after the contents of the body. And prepend before the contents of the body not before <body> e.g. in the head.

https://api.joomla.org/cms-3/classes/JResponse.html#method_appendBody

My code looks like this:

use Joomla\CMS\Application\WebApplication;

$thingToAppend = "<div>I am Eoin</div>";
appendBody::_($thingToAppend);

But when I use it I receive this error.

Error: Class 'appendBody' not found: Class 'appendBody' not found

What am I doing wrong?

After Mick's advice I tried:

WebApplication::appendBody($callpage_io__script);

But I got using $this when not in object context

So then I tried

$callpage_io__script = "<div>I am Eoin</div>";
$appendBody = new WebApplication;
$appendBody->appendBody($callpage_io__script);

Which did not error, but also did not produce any HTML

7
  • 1
    Syntax error. appendBody() is a method, not a class. api.joomla.org/cms-3/classes/… A static method name would come after two colons. JApplicationWeb::appendBody() See comments under question at joomla.stackexchange.com/q/323/12352 and this relevant answer with commentary from GeorgeWilson.
    – mickmackusa
    May 21 at 20:42
  • Ah ha. But then I have tried. WebApplication::appendBody($callpage_io__script); but then I get the error Error: Using $this when not in object context: Using $this when not in object context
    – Eoin
    May 22 at 13:28
  • So then I tried another route (updated question). No PHP error, but still no HTML either. Could it be because I am doing this in a module and it is arrive after the body has been created? So I would need to do this as a plugin instead? @mickmackusa
    – Eoin
    May 22 at 13:34
  • 2
    If your intention is to add content before </body> tag, see answer joomla.stackexchange.com/a/18551/12652.
    – Sharky
    May 22 at 16:55
  • Are you saying appendBody doesn't add it there? Surely it doesn't add it between <body> and <HTML> or after <HTML>
    – Eoin
    May 25 at 19:41
1

First of all, please note that JResponse::appendBody() is a depreciated method.

If you still want to use it, you can call it in a system plugin against the onAfterRender event.

Here is an example -

<?php
defined('_JEXEC') or die;

class PlgSystemMyPlugin extends JPlugin
{
    /**
     * Application object.
     *
     * @var    JApplicationCms
     */
    protected $app;
    
    
    public function onAfterRender()
    {
        if (!$this->app->isClient('site'))
        {
            return;
        }
        
        $this->app->appendBody('<div>I am Eoin</div>');
    }
}

This will add the "I am Eoin" string at end of the body i.e. after the tag.

<!DOCTYPE html>
<html lang="en-gb" dir="ltr">
<head>

</head>
<body>

</body>
</html><div>I am Eoin</div>

If you want to append your string before the closing tag then you need to use a custom plugin with the following codes -

<?php
defined('_JEXEC') or die;

class PlgSystemMyPlugin extends JPlugin
{
    /**
     * Application object.
     *
     * @var    JApplicationCms
     */
    protected $app;
    
    
    public function onAfterRender()
    {
        $doc = $this->app->getDocument();
        
        if (!$this->app->isClient('site') || $doc->getType() !== 'html')
        {
            return;
        }
        
        $buffer        = $this->app->getBody();
        $thingToAppend = '<div>I am Eoin</div>';
        $buffer        = str_replace('</body>', $thingToAppend . '</body>', $buffer);
        
        // Use the replaced HTML body.
        $this->app->setBody($buffer);
    }
}
10
  • So appendBody() is actually adding content after </html> instead of just before </body>? That's rather unintuitive.
    – mickmackusa
    May 24 at 10:44
  • There's no way anyone would ever want that @mickmackusa. That can't be right. @jumbo if you see my code I'm not actually using JResponse, I used the class mapping for J4 so I used Joomla\CMS\Application\WebApplication; but well done for mentioning it for others to see. What if I want to use this in a module? Is that possible?
    – Eoin
    May 25 at 19:39
  • stackoverflow.com/q/5958040/2943403 Also, I do not recommend using str_replace() or preg_replace() to modify an html document because they are vulnerable to over-matching and are both DOM-ignorant. As a simple case, if you have an article that contains the characters </body> (perhaps while explaining how to write htmls tags in a programming/tutorial article), then these php functions will make two replacements instead of one. My professional advice is to use a dom parsing technique to modify a html document.
    – mickmackusa
    May 25 at 22:02
  • @mickmackusa This shouldn't be a problem because in-content tags would have to be escaped on HTML pages. It's not valid to have more than 1 body tag anyways.
    – Sharky
    May 26 at 5:17
  • @mickmackusa It should not be a problem. Even core Joomla! System - SEF plugin uses preg_replace. Moreover, dom parser will not work here. It will break the HTML in most of the sites. You can try it yourself. Example: dom parser does not support empty HTML elements.
    – Jumbo
    May 27 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.