Cchost/developer/Hooking Pages

From Creative Commons
Jump to: navigation, search


Page rendering happens in 2 main phases in the CCPage class. In Phase 1 all the variables are gathered into a huge variable array. In Phase 2 CCPage calls down to it's base class, CCSkin, to merge the variables with the config-selected page template.

Right before Phase 2 happens, CCPage triggers an event to let any custom code prepare any last minute hacks before handing off to the template engine (CCSkin).

If you create a module that hooks that event you can manipulate the variables (er, hack at the array) to change the appearance of any/all pages.

An example: (put the following code with a PHP extension into your <local_files>/lib directory):


 CCEvents::AddHandler(CC_EVENT_RENDER_PAGE,  'page_trapper' );

 function page_trapper( &$page )
 {
    // This might be an array (I think)
    $caption =& $page->vars['page-caption'];

    // same with this
    $title =& $page->vars['page-title'];

    $caption = 'CAP: ' . $caption;
    $title = 'TITLE: ' . $title;
 }

The code above will simply append strings to the caption and title to every page in the system. If you are looking for a specific page you can for that page's title (e.g. 'str_file_deleting').

Of course your hacks are not limited to these variables. You can also, say, remove the bread crumbs from specific page by simply null-ing out the 'bread_crumbs' var:

 CCEvents::AddHandler(CC_EVENT_RENDER_PAGE,  'page_trapper' );

 function page_trapper( &$page )
 {
    $title =& $page->vars['page-title'];

    if( $title == 'str_file_deleting' )
    {
       unset( $page->vars['bread_crumbs'] );
    }
 }

In order to get a full idea of what variables are available you can inspect the page object:

  CCDebug::Enable(true);
  CCDebug::PrintVar($page);