I've seen this done in every version but 7.x.

I was thinking I could make a template for just my article nodes using "page-article.tpl.php" or "page--article.tple.php" but neither seems to work.

Comments

LaurentAjdnik’s picture

Try using __ (double underscore) instead of - or -- and let me know.

See http://drupal.org/node/224333#theme_hook_suggestions_2.

sethstevenson’s picture

That seems on the right track but that is specific to node template files which I can currently do with something like "node--article.tpl.php". However I would like to also use a different page template for different content types like "page--article.tpl.php".

sethstevenson’s picture

I ended up getting it to work with this

if (isset ($vars['node'])) {
		$vars['theme_hook_suggestions'] = array();
		$vars['theme_hook_suggestions'][] = 'page__'.$vars['node']->type;
	}

My main mistake was that in the documentation (http://drupal.org/node/224333#theme_hook_suggestions_2) they reference "$node->type" rather than "$vars['node']->type". Of course the name "$vars" is relative to your template_preprocess_page function.

LaurentAjdnik’s picture

All's well that ends well, as Shakespeare would say ;c)

Jeff Burnz’s picture

I updated the docs, since theres no variable assignment it really should be $variables['node']->type; in those docs (the example assumed $node = $variables['node']->type;).

http://drupal.org/update/modules/6/7#theme_hook_suggestions_2

stiknoltz’s picture

Thanks to everyone for your help here, came across this looking for a solution to this very issue and the above code solved when applied to template_preprocess_page, however I came across one caveat. Resetting 'theme_hook_suggestions' like this:

$vars['theme_hook_suggestions'] = array();

Can wipe out already existing suggestions [for example page--front.tpl.php]

Perhaps a check to see if the key already exists and is an array would be more prudent:

if (isset ($vars['node'])) {
    if (!is_array($vars['theme_hook_suggestions'])) {
        $vars['theme_hook_suggestions'] = array();
    }
    $vars['theme_hook_suggestions'][] = 'page__'.$vars['node']->type;
}
ideogram_nl’s picture

Do I notice correctyly that in the template function we have to use underscore's, whilst in the actual file name we should use dashes?!

jaypan’s picture

Yes.

Contact me to contract me for D7 -> D10/11 migrations.