开发者

Drupal 7 - What is the variable in template.php that dictates which page template is used?

开发者 https://www.devze.com 2023-02-24 02:51 出处:网络
Ok, here\'s the deal: I am constructing a Drupal website that has several different sections.Each section is a view that displays a content type. (Each section has it\'s own content type) For example,

Ok, here's the deal: I am constructing a Drupal website that has several different sections. Each section is a view that displays a content type. (Each section has it's own content type) For example, I have a view that points to ?q=blog which displays content type blog.

All the sections look a little different than each other. Not like 'website-within-a-website' different but different enough that they can't all use the same template file and each be modified with CSS. Each section needs it's own page.tpl.php.

Unfortunately, AFAIK Drupal theme's .info files can only either assign one page.tpl.php for the entire theme or assign a page-node-####.tpl.php for each node. There is going to be lots of content on this website so setting Drupal to make a new identical page-node-####.tpl.php for every created node would get unmanagable very fast.

To solve this problem, I am going to use pathauto to create an alias for each content type. For example, all nodes of content type blog are given an alias ?q=blog/[post title]. Modify template.php to use page-blog.tpl.php for any page who's alias starts with the word 'blog'.

Other people have tried doing this sort of thing and have created functions such as the one described. Unfortunately, all the ones I have seen are for Drupal 6 or below. I have tried modifying existing ones with no success. So far, though, I think this is on the right track:

function basic_preprocess_page(&$vars, $hook) {
    ...

    if( module_exists('path') ) {
        $alias = drupal_get_path_alias( $_GET['q'] );
        $site_section = "blog";
        if( strpos( $alias, $site_section ) === 0 ) {
            $VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE = "/path/to/page-blog.php";
        }
    }
}

I cannot find $VARIABLE_THAT_TELLS_THE_PAGE_WHAT_TEMPLATE_TO_USE does anyone know what it is?

Maybe my site is structured badly. If anyone knows how to restructure my site so I can more easily make a theme with seperate sections please share how!

Thanks a million! (c:

EDIT: Perhaps I need to use template suggestions instead. Does anyone know 开发者_如何学Gothe function or variable to use to set this?


They changed the name of this array key in D7 and I haven't seen it documented anywhere. I finally figured this out after a good bit of debugging. You can override the theme template in template.php with a hook_preprocess_page() like so:

function myTheme_preprocess_page(&$vars) {
global $node;

if ($node->type == 'blog') {
     $vars['theme_hook_suggestions'] = array('my__blog_template'); // use my--blog-template.tpl.php, note '-' = '_'
}
elseif ($node->type == 'articles') {
    $vars['theme_hook_suggestions'] = array('article__node_template'); // use article--node-template.tpl.php
}
}

Oh and don't forget to flush the Drupal caches after making changes to your template.php.


Ok, I found it: http://drupal.org/node/223440#comment-991840

$alias = drupal_get_path_alias($_GET['q']);
  if ($alias != $_GET['q']) {
    $template_filename = 'page';
    foreach (explode('/', $alias) as $path_part) {
      $template_filename = $template_filename . '-' . $path_part;
      $variables['template_files'][] = $template_filename;
    }
  }

Credit to this function goes to user mfb.

I had a lot of trouble with this so I will explain it here in case anyone finds it useful.

This function goes in your template.php. It needs to be part of the <theme name>_preprocess_page function. What it does is it takes the alias and then explodes it into a bunch of different components. For example if you are on a page with the alias ?q=blog/blog-post-title it would be exploded into blog and blog-post-title. It then turns each component into a name for a template suggestion. It puts each template suggestion into the template_files[] array (inside the $variables[] array) so that the page now has two new template suggestions:

page-blog, and page-blog-blog-post-title

Template suggestions are alternate template files. In this case they are for pages, but they don't necessarily have to be. You can have template suggestions for anything you can think of including blocks, nodes and the like. Don't let the name 'template suggestion' fool you. Template suggestions will be used over default templates as long as they exist. I don't know why it was named like that. I think it should be renamed.

What you do, then, now that you've set up Drupal to look for a template suggestion that points to your alias, is create a new template file where all the rest are in your theme. In this case, let's say I want to theme my entire blog section. In the templates folder I should create a file named page--blog.tpl.php (note the --double hyphens--) with the layout I want.

Drupal will use the most specific template suggestion it can find so if you wanted you could make one blog post to look completely different than the rest of the site long as you make a template for it named page--blog--blog-post-title and put it in your theme's templates directory. (again, note the double hyphens.)

0

精彩评论

暂无评论...
验证码 换一张
取 消