[Drupal] How to create a Drupal 7 theme from scratch

| | 3 min read

Theming with Drupal is extremely simple. You can build a simple theme from scratch. When you are developing a new theme, you have to follow certain methods i.e. creating a .info file and template files. If you want to create a custom Drupal 7 theme for your site, read the following tips.

Let us see how to create a basic Drupal 7 theme. Read Drupal.org to learn more about theming. Create a new directory inside Drupal sites/all/themes and rename it as YOURTHEME. This is nothing but your theme name. Inside this directory, create .info and other files.

Creating .info file

This file is important for any theme you create. You must name this file as YOURTHEME.info. This file tells Drupal the name of your theme. Also, if you are using style sheets or JavaScript, you must define them in this file. You can also define many other properties like region same file. However, these are optional .

A sample .info file.

core = 7.x
description = A simple theme with three column layout.
regions[header] = 'Header'
stylesheets[all][] = css/main.css 
scripts[] = js/script.js

The region you create will have a machine name and a human readable name. This region name will appear in the block settings if you want to assign blocks in a region.

Creating .tpl.php files

For your theme, you can create many .tpl.php files, which contain php variables and XHTML for your theme. Create a directory inside your theme directory and rename it to "templates". Inside the templates directory, you can start with page.tpl.php and node.tpl.php

Creating a page.tpl.php

In this file, you can create a custom design for all your Drupal pages. Create an html page with div's. You can give id's and classes for each div's. Next is rendering regions with variable. For example, you can render a region with

<?php print render($page['header']); ?>

Here the region name should match with the machine name that you have specified in .info file. In my example, "header" is the name of region. You can also theme specific pages for your site. For example, for front page, you can create a file with name "page--front.tpl.php"

Like you create page.tpl.php, you can also create a node.tpl.php but here you are going to theme all the node inside your Drupal site. So, you can start a div with node id. For example

<div id="node-<?php print $node->nid; ?>" class="<?php print $classes; ?> clearfix"<?php print $attributes; ?>>
/*-----theme your node here--------*/

You can use node variables to theme any node with node.tpl.php file. To theme a specific node, you can create another file with its name. For example, blogs can be themed with "node--blog.tpl.php" file.

Create a style sheet and script files for your theme. With CSS, you can style your theme with colors and images. You can also create a file called template.php inside your theme directory. In this file, you can override theme functions to fit according to your theme. You can read more about creating a template.php here.

If you do this much, a basic theme is ready for your site. You can style your theme with CSS, tpl.php files and JS files. I hope you found this article helpful. So what are you waiting for? Start building your own theme! Thanks. :)