onBeforeOutput

The onBeforeOutput handler works very similarly to the onOutput handler, except that it is run before widgets have been processed (i.e., they still appears as <widget id="... in the $buffer).

Furthermore, as the below example indicates, REGISTERED_BODY_CLASSES haven’t yet been added to the page HTML, so you can still edit that array.

<?php
    $_LW->REGISTERED_APPS['my_app']=[
       'title'=>'My App',
       'handlers'=>['onBeforeOutput'],
    ];

    class LiveWhaleApplicationMyApp {

      public function onBeforeOutput($buffer) {
      global $_LW;

        // add page_id_123 to the body class
        $_LW->applyPageAndGroupVars('<xphp var="page_id"/>');
        if (!empty($GLOBALS['page_id'])) {
          $_LW->REGISTERED_BODY_CLASSES[]='page_id_'.$GLOBALS['page_id'];
        };
        return $buffer;

      }

   }
?>