phpdrupaldrupal-7hook-menu

Drupal 7: print theme from hook menu with good performance


I'm working in a hook menu for a custom route, like this:

function mymodule_menu() {
  $items = [];
  $items['myroute/%'] = array(
    'page callback'     => 'my_callback',
    'page arguments'     => array(1),
    'access arguments'  => array('access content'),
  );
  return $items;
}

In the theme_hook, I added a new template feature, like this:

function mymodule_theme($existing, $type, $theme, $path) {
  $default = array(
    'path' => drupal_get_path('module', 'mymodule') . '/templates',
    'variables' => array(),
  );
  return array(
    'product_tile' => array_merge($default, array(
      'template' => 'product-tile',
    )),
  );
}

I created a template file called 'product-tile.tpl.php', which is working fine for all situations and is a partial template.

In the callback function, I need to return a specific .tpl.php template, like this:

function my_callback($parameter) {

 $template_data = 'lorem ipsum';
 $output = theme('product_tile', array('content' => $template_data ));
 echo ($output);

}

The point is: the 'theme()' function is taking too long to render data and it renders not only the template, but the whole html structure, which is not needed and is not part of the template.

Ex: template is:

<div id="my structure></div>

But when i get my response to '/myroute/myparameter, instead of printing my template, it prints all html structure like this:

<html>
  <body>......lots of stuff here +js +css + all drupal stuff
      <div id="my structure></div>
  </body>
</html>

And takes a lot of time to print (like 10s or more).

I tried to cache it by using cache_get and cache_set, but strange things are happening, like random empty responses.

Does anyone know a more performant way to print a partial template in a hook menu in drupal 7? It's terribly slow this way.

Thanks in advance


Solution

  • Your custom route is mapped to a page callback, a function that must return content to be rendered inside the page to deliver (not just print something).

    This content can be either :

    That content (once rendered if not an HTML string) is what you actually get in the $content or $page['content'] variable being passed to the active theme's page.tpl.php.

    Now, your issue may be caused just by using this template (if it contains heavy PHP or bad implementations for example, whatever the reason), but it could be something totally different :