Using PHP to Dynamically Switch Drupal Themes and Templates

One powerful trick to improving the way a drupal site flows is switching page templates when certain PHP arguments return true. This is a quick tutorial on how to do that.

Step One: make three copies of page.tpl.php and the name them "frontpage.tpl.php", "admin.tpl.php", and "inside.tpl.php".

Step Two: Open page.tpl.php with an editor. Erase everything.. and I do mean everything, we're building this from the ground up. (get the coffee ready)

Step Three: copy the following code into page.tpl.php:

<?php if (arg(0) == 'admin') { include "admin.tpl.php"; } if ($is_front) { include "frontpage.tpl.php"; return; }else { include "inside.tpl.php"; ?>

Step Four:Test to make sure that indeed your different templates are

1. admin pages, 2. the front page, 3. and all other pages.

Step Five: Use these methods, love these methods, and cherish these methods.

Notes: If your curious as to what arg(0) does, add this code somewhere in your body.

<?php $satan = arg(0); $gwbush = arg(1); $karlrove = arg(2); print $satan; print ""; print $gwbush; print ""; print $karlrove; print ""; ?>

Click around, and you'll figure it out.