1
votes

Im working on my first site in drupal and I have also read some basics of theming and module development. Now I am creating (overriding stark theme) my own theme, i.e. page.tpl.php and there is an theme() function called for outputting main menu items:

 <?php print theme('links__system_main_menu', array('links' => $main_menu, 'attributes' => array('id' => 'main-menu', 'class' => array('links', 'inline', 'clearfix')), 'heading' => t('Main menu'))); ?>

I roughly understand what this function is for, but why should I use it in this case? It would make sense if outputting data from module - to stylize that output by selected theme. But in this case everything I need is directly in $main_menu array and I can stylize it however I want, so what's the use for theme() function in page.tpl.php?

3

3 Answers

3
votes

Why should I use theme() function in page.tpl.php?

You shouldn't.

To avoid calling theme() function in your page template, you can do this in your template.php:

/**
 * Implements hook_preprocess_page().
 */
function yourtheme_preprocess_page(&$vars) {
  $vars['main_menu'] = theme('links__system_main_menu', array('links' => $vars['main_menu'], 'attributes' => array('id' => 'main-menu', 'class' => array('links', 'inline', 'clearfix')), 'heading' => t('Main menu')));
}

And then simply print $main_menu; in your page.tpl.php.

2
votes

The point of using theme('links__system_main_menu', ...) is to re-use the existing generic links theme implementation of the theme. The system_main_menu suffix (after the two _), allow you to provide a more specific implementation of the generic links. If you then override the page template for the front page (ie. page-front.tpl.php) and for nodes of a specific content, you don't have to duplicate your HTML code. Which make it easier to maintain since you won't have to duplicate changes to multiple files.

0
votes

The point is that if you go trough drupal's theme system it gives a chance to other installed modules to do their changes - their hook functions will be called:

https://www.drupal.org/node/933976

In other words, if you don't do it "clean" way it may happen that some other's module feature won't work.