This is an introduction on how to convert a modern CSS/HTML design to PHPTemplate, the Bryght template language (In addition to PHPTemplate, Drupal has lots of other templating languages; Bryght only offers PHPTemplate because we think it is the most flexible and the best and of course, a Bryght guy :-) wrote it!). It is an introduction and it is therefore not exhaustive in its treatment of PHPTemplate. But, together with the the official PHPTemplate documentation, you should have enough information to convert basic CSS/HTML designs to PHPTemplate and after reading this, you should have a solid foundation for more advanced conversions.
Click on "read more" for the full details!
Don't despair if you don't know the above or don't understand the web pages that are linked to above. They will be explained in the context of converting a CSS/HTML design below. If you can create a CSS/HTML design, you can easily master the basics of PHP that are required by PHPTemplate!
This will be done by customizing one of the existing themes (it doesn't matter which since you will be overriding that theme's files!) which creates a copy of an existing theme and then renaming the copy:
Your design probably has some supporting graphic files (usually in JPEG, GIF or PNG format). If so, for each graphic file:
First, you will save a copy of the template's CSS file which is usually called "style.css". Why? To act as a reference and a backup:
Then you will replace the template's CSS with the CSS of your design:
In your favourite text editor, open your design's CSS file, select all the text and copy into the browser window with style.css » "Save
First, you will save a copy of the template's page.tpl.php file. Why? To act as a reference and a backup:
Then you will replace the template's page.tpl.php with the HTML of your design after first editing it to replace content with PHPTemplate variables as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Welcome to Acme Ltd.</title>
<link href="mydesign.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/javascript" src="/js/auto.js"></script>
</head>
<body>
becomes (note the variables in bold; in particular that "Welcome..." has been replaced by $head_title and "mydesign.css" has been replaced by "print $styles"; when PHPTemplate sends a page to a web browser, it substitutes the title of the web page for $head_title and the path to the stylesheet for $styles and so on):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="<?php print $language ?>"
xml:lang="<?php print $language ?>">
<head>
<title><?php print $head_title ?></title>
<meta http-equiv="Content-Type" content="text/css" />
<?php print $head ?>
<?php print $styles ?>
</head>
<body <?php print theme("onload_attribute"); ?>>
<img src="develop.jpg" width="110" height="90">becomes
<img src="<?php print $directory;?>/develop.jpg" width="110" height="90">Why do this? The
$directory variable is a special one that outputs the path to your theme directory where you have uploaded your images.Add a PHP foreach loop for navigation bars.
Drupal stores the links for the navigation bars in PHPTemplate variables e.g. for primary navigation, the variable is $primary_links and the for secondary navigation the variable is $secondary_links. Check out our How to Edit Your Site's Navigation Links for more info on this.
Here's a primary navigation example:
<div id="navlist">becomes (note that the hardcoded about.htm has been replaced by $primary links, note the bold text)
<ul>
<li class="on">Home</li>
<li><a href="about.htm">About Acme</a></li>
<li><a href="solutions.htm">Solutions</a></li>
<li><a href="support.htm">Support</a></li>
<li><a href="news.htm">News & Updates</a></li>
<li class="input"><input class="form-text" type="text" size="10" value=""
name="keys" /> Search</li>
</ul>
</div>
<div id="navlist">
<?php if (is_array($primary_links)) : ?>
<ul id="primary">
<?php foreach ($primary_links as $link): ?>
<li><?php print $link?></li>
<?php endforeach; ?>
<?php if ($search_box): ?>
<li class="input"><form action="<?php print $search_url ?>"
method="post" id="searchform">
<input class="form-text" type="text" size="10" value=""
name="keys" id="s" /> Search
</form></li>
<?php endif; ?>
</ul>
<?php endif; ?>
</div>
This may seem "challenging", but it's not. You are replacing hardcoded links in your navigation bar with links that Drupal supplies when serving a web page to a browser. The PHP is_array function simply checks to see if a variable (in this case $primary_links) is an array which is PHP's term for a list with one or more members.
If an administrator has defined some primary links, then $primary_links will be an array. In that case, the PHP foreach statement is used to retrieve each of the links that the administrator has defined. In our example, the administrator would define the links as support, solutions, about and news. The ".htm" extension is not needed for Drupal.
If an administrator has not defined any primary links, then $primary_links will NOT be an array and a nav bar will not be generated.
The $search_box and $search_url are used to set up a search box. If you would like a search box as part of your nav bar, simply copy this code. More details about the PHPTemplate search related variables can be found on the page.tpl.php documentation page.
<?php if ($is_front): ?>
... front page specific stuff
<?php else: ?>
... stuff for the other pages
<?php endif; ?> <!-- /! is_front -->
$is_front is set to true if Drupal is rendering the front page, false otherwise. So you use the above code to put up front page specific markup if you have any (not all themes do!).
For example you may only want to display the site mission and slogan on the front page. Here's one way you could do do that:
<?php if ($is_front): ?>
<div id="homecontent">
<?php if ($site_slogan) : ?>
<h1 id="site-slogan"><?php print($site_slogan) ?></h1>
<?php endif;?>
<?php if ($mission) : ?>
<?php print($mission) ?></div>
<?php endif; ?>
<?php endif; ?>
Note that the above code is just an example. You can call the CSS styles whatever you want and put the slogan and mission anywhere on the page.
<?php print $closure;?>
You do this by setting your theme to your new theme while you are logged in as a user with admin privileges. Before you do this, make sure you are not logged in as admin. Again, by not using admin, but using another id with admin privileges, you can recover by logging in as admin should a bug in your theme make the site unusable or unreadable.
Why do you have to do this? Well, extension modules that implement content types may add their own styles.
For all the content types that you use on your site (blog post, story, event, etc.), view a sample page with that content type and see if everything is styled. If not, view source, find out the style and change style.css or whatever the filename of the CSS stylesheet is.
Once you are satisfied with your theme, you must activate it for everybody as follows:
The power of PHPTemplate is only limited by your imagination. There's plenty more you could do (e.g. we haven't discussed some PHPTemplate variables that you probably want to use in your template). You have the flexibility and power of CSS and PHP at your fingertips.
This is only a introduction so we have only discussed the page template. Depending on your needs you may want to modify the other templates:
Consult the official PHPTemplate documentation and go for it!
This work is licensed under a Creative Commons Attribution-Share Alike 2.5 License. This license applies to all text written by Bryght. All others retain full copyright to their text.