3

I’m developing a new Drupal 6 theme with a custom front page. My standard page.tpl.php has the entire HTML structure (as it should), including the doctype, HTML head, and HTML body.

When I make page-front.tpl.php, though, I have to duplicate page.tpl.php and modify it, meaning that whenever I need to make changes to the standard page template (add Javascript, modify CSS, etc.), I have to replicate all those changes in the front page template.

What’s the best way to avoid all this duplication? What’s the best way to keep the main page structure uniform? Create a custom function that spits out the HTML head? Put everything in separate PHP files (header.php, footer.php, etc.) and include them in the two template files?

3

To avoid duplication, creating separate PHP files might work, but the best answer is to you do everything in your MYTHEME_preprocess_page(&$variables) function, in your template.php file.

MYTHEME_preprocess_page(&$variables) {
  if($variables['is_front']) {
    //set variables to X
  }
  else {
    //set variable to Y (or just leave them)
  }
}

Now you probably won't even need page-front.tpl.php

Also, if you are using page.tpl.php and/or page-front.tpl.php you do have the variable $is_front available

2

Not sure exactly what you are trying to do different, but my suggestion would be to use Panels.

When you do this, your page.tpl.php can stay pretty generic, mainly header footer stuff, and then you can have different panel layouts for your different HTML layouts.

Another options is to do things like

<?php if ($is_front): ?>
  <p>I'm on the front page.</p>
<?php else: ?>
  <p>I'm not on the front page.</p>
<?php endif; ?>

inside page.tpl.php.

Also remember that the body normally has class on it called front when it is the front page, and not-front otherwise. This can help a lot with targeting CSS and/or jQuery.

2

I was doing the very same thing and after few attempts with the page-front.tpl.php method, I discovered that the best and most flexible solution for me was the front page module, where you can set several types of home page:

  1. full - sets the arbitrary HTML code of the whole page, if you want it to be completely different from the theme
  2. themed - sets the arbitrary HTML code just for the content, while the rest of the page will follow the active theme
  3. redirect - sets a URL that replaces the home page

In full and themed types you also have an option to insert PHP code too within your HTML and enable its execution - extremely flexible!
You can also set custom home pages by roles and set temporized content for people not visiting your site for a certain period of time.
Hope it can help.

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.