custom callback.

Events happening in the community are now at Drupal community events on www.drupal.org.
dvessel's picture

I was thinking about a custom callback function to do 3 things:

  1. Speed up execution time. Remove any intermediate functions that aren't needed. Go directly to the renderer from the callback. (I know you've been thinking about this one merlin. I'd like to see your solutions to this.)
  2. To organize the directory structure. Instead of calling the subdirectory through the hook, have a separate parameter for what it's parent section or parent function is. I think this makes more sense. We could even make a 'templates' subdirectory by default to prevent the cluttering of the base theme directory.
  3. The ability to return a null if the .tpl.php file isn't found so we can pass the original theme_ function. A fallback would be nice to prevent silly errors. This is easy to do since the files are checked for in the callback.

example:

<?php
/**
* Simplified version of <em>phptemplate_callback().
*/
function _custom_callback($hook, $vars = array(), $parent_hook = '') {
  global
$theme_engine;
 
$file = path_to_theme() .'/'. ($parent_hook ? $parent_hook .'/' : '') . $hook .'.tpl.php'
  if (
file_exists($file)) {
    return
call_user_func('</em>'. $theme_engine .'_render', $file, $vars);
  }
}
?>

And we call it:

<?php
function phptemplate_breadcrumb($breadcrumb) {
  if (!empty(
$breadcrumb)) {
   
$vars = array(
     
'breadcrumb'  =>  $breadcrumb,
    );
   
// Get the output. Also pass the parent section or whatever category we want.
   
$output = _custom_callback('breadcrumb', $vars, 'system');
   
// Return based on the state of $output.
   
return isset($output) ? $output : theme_breadcrumb($breadcrumb);
  }
}
?>

I think this would help with structuring .tpl.php files that are broken off from loops also. We could have the parent function in the $parent_hook parameter and drop the .tpl file into it's own subdirectory. Underscore it so it's easy to identify that its' not a real theme function.

This is from theme_block_admin_display() which loops through, adding some html:

<?php
 
// Highlight regions on page to provide visual reference.
 
foreach ($block_regions as $key => $value) {
   
drupal_set_content($key, _custom_callback('_regions_highlight', array('region' => $value), 'block/block_admin_display'));
  }
?>

It would look inside "NameOfTemplate/block/block_admin_display/_regions_highlight.tpl.php".

This is a simple approach and I think we should nail this down sooner than later. Converting all the theme functions could depend on how the custom callback is done and could lead to more work later. Or do we care about making it custom? What are your thoughts?

Comments

Bear in mind that I'm

dvessel's picture

Bear in mind that I'm learning as I go. Point #2 was, well... pointless. Using the Just the $hook works well.

I'm experimenting with this idea in a theme I'm working on. The call back function (not even sure if it's fit to be called that anymore) is this:

<?php
function _lumen_callback($hook, $vars = array()) {
 
$file = path_to_theme() .'/templates/overrides/'. $hook .'.tpl.php'
  return
file_exists($file) ? _phptemplate_render($file, $vars) : NULL;
}
?>

I haven't bench marked it but it's fast -I'm also using an opcode cache so it's hard to tell the difference. But on another note, the memory usage will be high putting all the business logic inside the template.php file. The html is usually minimal.

I was getting ahead of

dvessel's picture

I was getting ahead of myself. Nothing to see here.. Move on.

According to the dojo podcast on theming, this would be phase 3.. We're nowhere near getting phase 1 done.. I digressed.

Themer Pack Working Group

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: