Hi! I'm trying to find a user-friendly way to be able to specify a different theme for specific pages through the admin interface, so a non-techie site admin doesn't have to do any template coding.

The best idea that I've been able to try is to use the template-switching snippets in Customising the page layout based on path, and setting a unique path alias for those pages. In my case, I set the page alias to "store/product1" and the template to switch on arg(0)=="store". Unfortunately arg(0) doesn't seem to apply to path aliases, so it doesn't work.

Do you have any other ideas that I could try? I've thought of switching templates based on taxonomy, node number, etc. but it has to be something that can be activated without editing the template, and so far all of the methods I've tried require new nodes/terms to be added to the logic in the template.

Thanks in advance!

Comments

Ryanbach’s picture

If you don't get the information here, maybe you should comment here: http://lullabot.com/articles/hacking_phptemplate ?

ob3ron’s picture

Thanks! Done. I'll post back here if I get a solution.

TheEdge’s picture

I asked the same question and got some good feedback. Check out: http://drupal.org/node/55075

ob3ron’s picture

Thanks, TheEdge. I had seen most of those resources already, but so far I still haven't been able to do what I want. I may be missing something obvious, but I don't see a way in any of those methods to set a "switch" within the admin interface to make a page use a different template. It's easy to do if you can edit the template directly, but I want to set it up for a non-techie site admin so they don't have to touch the template.

I haven't fully investigated setting up a new node type to use a different theme, so that will likely be where I focus my next experiments. I'll keep you posted!

BlackSash’s picture

An option for you might be to use a different variation on the lullabot article: it's something I use to apply different styles to different sections. It uses clean urls and pathauto for selecting a template to use.

  $uri_request_id = $_SERVER['REQUEST_URI'];
  $section = explode("/", $uri_request_id);
if ($section[2] == "section1") {
  include 'page-section1.tpl.php';
  return; }
if ($section[2] == "section2" {
  include 'page-section2.tpl.php';
  return; }
include 'page-default.tpl.php';
return;

This will select a page template to use based on the actual address of the page, hence the dependency on clean urls. pathauto is not a requirement, but it does make it easy to automatically categorize your new content into one of the sections.
If none of the rules match, a default template is passed through for the site to use.

Once this is done (put in page.tpl.php, remove everything else and paste that in page-section1.tpl.php and page-section2.tpl.php) you can use the section templates with custom CSS, body tagging and a host of other tricks.

One important thing to note about this system though:

if ($section[2] == "section2"

change the [2] into the actual component of the url you want to use, with the / acting as a delimiter. (http:// is not counted)
so for
http://my.site.tld/drupal
would require
if ($section[1] == "section2"

and
http://my.site.tld/drupal/my_section
would require
if ($section[2] == "section2"

and so on.
---
GU d- -p+ c++++ l++ e* m* s+ n+ h+ f? g+ w+++ t- r y?
Junior developer @ Madcap B.V.
http://www.madcap.nl

Ryanbach’s picture

It may be easier to use a switch statement, for example:

$uri_request_id = $_SERVER['REQUEST_URI'];
$section = explode("/", $uri_request_id);

switch ($section[2]) {
case: 'section1':
include 'page-section1.tpl.php';
break;
case 'section2':
include 'page-section2.tpl.php';
break;
default:
include 'page-default.tpl.php';
}
ob3ron’s picture

That's true, it is more elegant that way. The main thing is that it works.... having elegant code is just gravy.

Thanks for your help!

murph’s picture

I get an error trying out either of these pieces of code. With yours Ryanbach i get this:

Parse error: parse error, unexpected ':' in /home/mydomain/public_html/themes/friendselectric/page.tpl.php on line 5

My page.tpl.php file just has this code inside...everything else removed.

ob3ron’s picture

There is an extra colon in the layout1 line. Should be:

case 'section1':

ob3ron’s picture

Thanks, BlackSash! That was what I needed. I didn't get the distinction between using arg() and explode() depending on whether the site uses clean urls -- which mine does.

I think I'll post this into the handbook pages so others don't have the same trouble.

Thanks again!

murph’s picture

If i have a section of my site that uses path aliases of:

mysectiona/*
mysectiona.html

Would it be:

if ($section[mysectiona/] == "section2"
.
.
.

if ($section[mysectiona.html] == "section2"

Thanks

ob3ron’s picture

Almost. If you use a path alias:

mysectiona/mypage.html

it would be:

if ($section[1] == "mysectiona") {
.
.
.
if ($section[2] == "mypage.html") {

This page should help clarify as well: http://drupal.org/node/46027

murph’s picture

Even if it is like this:

mydomain/mysectiona/*
mydomain/mysectiona.html

Wouldn't they both be [1]?

ob3ron’s picture

Not quite. The line that says:

$section = explode("/", $uri_request_id);

splits your URL into an array, based on its segments that are separated by a slash. So if your path is mydomain.com/mysectiona/mypage.html, then you would get:

$section[0]=="mydomain.com"
$section[1]=="mysectiona"
$section[2]=="mypage.html".

In your two examples, you would get:

mydomain/mysectiona/* --> $section[1]=="mysectiona"
mydomain/mysectiona.html --> $section[1]=="mysectiona.html".

Make sense?

---
Arvana
arvanadesign.com

murph’s picture

Clear now, thanks again. :)