Tutorials PHP Drupal Themes

Drupal Themes

I haven't updated my tutorials in a while because I got a new job! They're very Drupal oriented so I've had to learn how to create themes.

What files do you need?

You only need two files for the theme to work. page.tpl.php and a *.info file. The filename of the *.info should be the same name as your theme.

What is the *.info file?

This file contains the basic information for your theme. Information like the name of the theme, description, regions, and features are containted within this file.

A basic *.info file:

name = My Theme
description = Custom theme for mytheme
core = 7.x
engine = phptemplate

regions[content] = Content
regions[sidebar] = Sidebar

stylesheets[screen][] = css/reset.css
stylesheets[screen][] = css/style.css

scripts[] = js/scripts.js

features[] = logo
features[] = main_menu
features[] = slogan
features[] = secondary_menu
features[] = favicon

What is the page.tpl.php file?

This is the basic template for your site. Everything you need to structure your site is in this file. There are a lot of variables you can utilize in this file, and most are pretty self-explanatory. For an extensive list of the variables, visit: Drupal.

A basic page.tpl.php file:

<div id="wrapper">

<div id="header">
<?php
// Check if there is a logo and echo it
if ($logo) {
  echo '<a href="'.$base_path.'"><img src="'.$logo.'" alt="'.$site_name.'" id="logo" /></a>';
} // End logo check
?>
</div><!-- End Header -->

<div id="navbar">
<?php
echo theme('links__system_main_menu', array('links' => $main_menu, 'attributes' => array('id' => 'menu-list')));
?>
<div class="clear"></div>
</div><!-- End navbar -->

<div id="container">

<div id="sidebar">
<?php
echo theme('links__system_secondary_menu', array('links' => $secondary_menu, 'attributes' => array('id' => 'secondary-list')));
echo render($page['sidebar']);
?>
<div class="clear"></div>
</div><!-- End sidebar -->

<div id="content">

<?php print render($title_prefix); ?>
<h1 class="page-title"><?php echo $title; ?></h1>
<?php print render($title_suffix); ?>

<?php echo render($messages); ?>

<?php echo render($tabs); ?>

<?php echo render($page['content']); ?>

<div class="clear"></div>

</div><!-- End Content -->

</div><!-- End Container -->

<div id="footer">
</div><!-- End footer -->

</div><!-- End Wrapper -->

What are some common optional files you can have in your theme?

One of the most common files to put in a theme is a screenshot.png that displays when you select that particular theme from Drupal. You can also put in a logo.png that Drupal will use as your default file. You can also add a template.php file to your directory that Drupal will read before it processes the rest of your theme.

Download

For a starting template, you can download a a basic template and tailor it to your needs.

Download

That's all for today's tutorial for Drupal. I'm still learning the ropes and it doesn't seem as hard as I thought. However, I'm still sticking with my belief that no mainstream content management system will ever beat a custom-built one.

Posted by on . Category: PHP


Comments

No comments posted yet

You need to register or login to post new comments.