Using PHP to Dynamically Switch Drupal Themes and Templates

07.18.2005

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:

<?phpif (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 "<br />";print $gwbush;print "<br />";print $karlrove;print "<br />";?>

Click around, and you'll figure it out.

Comments

<?php$satan  = 

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

where to add this code
I have added in admin.tpl.php
but I have not seen any changes.

great tip, and slight correction

I am brand new to drupal, and this tip has been quite handy! I added a couple of things, a "return" after admin.tpl.php and the closing brace for the else: <?phpif (arg(0) == 'admin') {    include "admin.tpl.php";    return;} if ($is_front) {    include "frontpage.tpl.php";    return;}else {    include "inside.tpl.php";}?>

its a bit inflexible

at first .. very nice tutorial second, for more complex themedesigns its a bit inflexible (or was it unflexible ?) just imagine a case, where you want different node.tpl.php or block.tpl.php or even flexinode-xxx.tpl.php you would need a huge "case of" structure inside your node.tpl.php (and it has to work with the structure in the page.tpl.php) i like it centralized so ... i would use the template.php for switching the templates check http://www.drupalcenter.de/node/100 for a code example (to use it for the page.tpl.php, you just need to use the $hook = page construction) within the template.php you can easily grab the right .css for your site an working example is my own homepage http://www.langmi.de, for the front i switch to my own templates (the rest is styled in bluemarine, didn had much time to work on it yet) http://www.langmi.de/themebrowser is an example for switching themes with a module if your interested in code examples, just contact me Michael

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options