2

I came to this query while trying to create my own theme. Say i have my region named footertop, which is placed above the footer. Its going to contain only some html content and some site logos which are static atleast for now.

I don't have any blocks to assign to this region but still want it to appear and had created a template file named block-footertop.tpl. But to my understanding (from drupal docs) this region only appears if any block is assigned to footer top region and is meant to be that way. My page.tpl.php doesn't have any conditions placed for this region, merely

<?php print $footertop; ?>

I'd like to know if there's any way to 'include' the template within page.tpl.php like its done with smarty. Most of the search i'd done showed me results containing *hook_theme()* and seemed different than what I'm trying to achieve.

Why I'm trying to this is to save a big bunch of html markup from being in my page.tpl.php and sheer curiosity to understand drupal theming better.

Any suggestions/solutions to this novice drupal enthusiast would be greatly appreciated.

Thanks in advance!!

2

First of all I think you should be sure you know the difference between regions and template variables. Second, there are different approaches to your question:

A) Custom region + block

You could indeed define a new region. This can be done in one of the two ways:

  • in your theme's .info file (best way) like this regions[regionname] = Region name
  • in the template.php file (haven't tried or felt the need to use this one)

After you defined your new region you can add blocks and content to it. You could just skip defining a new region and just add your block to the footer region or the region appropriate to your needs.

In your case your myTheme.info file would look something like this:

; $Id$
; @file: myTheme.info

regions[footertop] = Footer Top
  • output the region variable ($footertop)

    /**
    * @file page.tpl.php
    * $footertop contains all the HTML content for that region
    * and the blocks belonging to that region
    */
    print $footertop;
    
  • .info file is cached in the database, so you need to clear the cache

  • add a new block: www.example.com/admin/build/block/add
  • add the block to the footertop region: www.example.com/admin/build/block/list/myTheme

When should you use this approach: when you have static content or/and just some snippets of text/html you want to add to your site. Remember to choose an appropriate input filter, otherwise some/all of your content might get filtered out.

Source: http://drupal.org/node/171224

B) Template variable

We want to define a new general variable available for all templates. Something like $content or $is_front. How do we do this: good old friend hook_theme :)

Create a new template file footertop.tpl.php and in it add the static data you want to add

/** 
* Implementation of hook_theme()
* @file template.php - in your active theme
*/
function myTheme_theme(&$existing, $type, $theme, $path) {

  /*
  * If you are sub-theming another theme, like Zen,
  * you might need to do something like this
  * $hooks = zen_theme($existing, $type, $theme, $path);
  */ 

  $hooks['footertop'] = array(
    'template' => 'footertop',
    // 'arguments' => array(), // We could specify some arguments, useful when the content is not static
    // 'path' => $path . 'to/file' // This is also usefull if your template is in a subfolder
  );

  return $hooks;  
}

/**
* Now we make our variable available to other templates
* file template.php - in your active theme
*/
function myTheme_preprocess_page (&$vars, $hook) {
  $vars['footertop'] = theme('footertop');
}

Now to add the variable in a template just do this:

/**
* @file e.g. page.tpl.php
*/

/* Some other code */

<?php print $footertop; ?>

/* Other code */

Source:

Now if you want to be a pro with drupal theming or development you should get yourself a book on this matter. It helped me a lot when I was struggling to understand how Drupal theming works :) You'll defined find the right one for you.

  • thanks so much for explaining hook_theme in one of the best possible way..in my perspective template is the skeleton and blocks are constituent, regions placeholders for blocks...i've read the documentation of hook_theme quite a few times..but it took me no where....you nailed it!!.. i'm quite clear on A) Custom region + block because thats what i've been doing for awhile now.. anyhow thanx very much! – optimusprime619 Nov 28 '11 at 18:48
  • actually template.php is where the output is processed. So more like the brain, behind the scenes. Check this page out: drupal.org/node/171194. It has a really nice and visual overview of theme files. Glad I managed to help you :) – Max Nov 30 '11 at 12:38
  • was referring to .tpl files :) small miscommunication... anyhow thanks!! – optimusprime619 Nov 30 '11 at 19:27
  • Excellent answer! One clarification for noobs: the 'path' is crucial if your template file is under, say, themese/<my-theme>/templates. If omitted you'll be scratching your head! I spent an hour debugging this, until I realized that I had forgotten this line. – cybertoast Dec 14 '12 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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