Cchost/developer/Hooking Pages

From Creative Commons
Revision as of 16:37, 3 November 2008 by Fourstones (talk | contribs) (New page: Category:ccHost Category:ccMixter Category:Developer Category:Software Category:Technology {{lowercase}} Page rendering happens in 2 main phases in the CCPage class. I...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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);