I'm trying to create a dynamic site based on a static theme. I wonder to know why when I want to put my main menu in the region--page-top.tpl.php it does't work. this code

<?php print theme('links__system_main_menu',
                   array('links' => $main_menu, 
                            'attributes' => array( 'class' => array('links','inline'))));?> 

works correctly inside the page.tpl.php but it seems that php code doesn't recognize in the region section. The HTML codes work fine, though.

  • 1
    Because the $main_menu variable is not available to region--page-top.tpl.php template.
    – Alex Gill
    Feb 25, 2014 at 10:44
  • Your making this difficult than it needs to be, just forget the region and incorporate it into your page.tpl
    – Alex Gill
    Feb 25, 2014 at 10:49

1 Answer 1


I believe you cannot render main menu in this way within a region, the variable is not there. You could try with:

$main_menu_tree = menu_tree(variable_get("menu_main_links_source", "main-menu"));
print drupal_render($main_menu_tree);

Even better, I would recommend that you simply render the output of the particular region properly and add the menu dynamically from Blocks administration.

To render a region in your theme shortly, you need to define a proper region in your theme's .info file (eg. my_region) and render it using print render($page['my_region']); in the .tpl.php file. I hope this guide will help you: https://drupal.org/documentation/theme

  • Thanks Wtower. I would appreciate if you tell me how could you find that we can not render main menu in the region. is it in the documentation and what is the right approach for building a site. I just wanted to split a region and put them in their theme file. for your recommendation would you please give me some information how can I add the menu dynamically I didn't get the point(is there any references for that). Thanks again Feb 25, 2014 at 16:44
  • 1
    Welcome @MohammadYousefi. I didn't say you cannot, I said that you cannot render it in the way you described because there is no $main_menu variable defined in your file. You need to define a proper region in your theme's .info file (eg. my_region) and render it using print render($page['my_region']);. I hope this guide will help you: drupal.org/documentation/theme . Updating my answer.
    – Wtower
    Feb 25, 2014 at 17:14
  • Sorry mate that I bother you. I have read the documentation and that was very useful. I had chosen the page_top because it is kind of Drupal system type and they are part of the page, and page will always be rendered (in contrast to header and footer, region ) and also I defined them into my .info file. though it seems that I should write a preprocess function to pass the page variable (main_menu) into my region. does this approach and using share variable cause a performance issue? and does the code that you gave me should be placed in the region file (instead of writing preprocess function) Feb 26, 2014 at 8:14
  • 1
    No bother at all. You can try to use this piece of code that I gave you within the <div> element you believe it is appropriate, in the .tpl.php file (as edited). There is no need for preprocess functions or other hooks and no significant performance considerations.
    – Wtower
    Feb 26, 2014 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.