1

I have a custom tpl page intended for a specific page like so:

html--something--anotherthing.tpl.php

This allows some custom handling on the page /something/anotherthing as desired. Unfortunately it is also being applied to sub pages like /something/anotherthing/details where the custom handling is not desired. Is there a syntax for naming tpl files so that it only applies to the desired page?

edit: I've enabled theme_debug and it suggests exactly what I already have for the filename so not helpful.

2

If it's just one page you can just use something like node--nid.tpl.php or page--node--nid.tpl.php right? (Assume that /something/anotherthing and /something/anotherthing/details are two difference nodes)

  • The question mentions page but the example template file names suggest OP is overriding the html template rather than page. It's the same deal, just html--node--1.tpl.php instead of page--... – Clive Jan 23 '18 at 20:40
  • The page in question isn't a node. More of a custom page full of mini panels. – Anonymous Man Jan 23 '18 at 20:40
1

If /something/anotherthing is a node, the answer by cchen has you covered. In case it's not...

The base suggestions are built up in theme_get_suggestions, (called by template_preprocess_html), and mirror the path and its parents. So for a path of /foo/bar/baz, you'll automatically have:

  • html
  • html__foo
  • html__foo__bar
  • html__foo__bar__baz

You can approach changing that in a couple of different ways:

  1. Don't use the built-in suggestions, create your own:

    function MYMODULE_preprocess_html(&$vars) {
      // Example only, if you have a better way to determine the path, use it!
      if (request_uri() == '/something/anotherthing') {
        $vars['theme_hook_suggestions'] = 'something__anotherthing';
      }
    }
    

    A template file named something--anotherthing.tpl.php would be picked up for that path only.

  2. Remove the built-in suggestion for the parent when visiting the child:

    function MYMODULE_preprocess_html(&$vars) {
      if (request_uri() == '/something/anotherthing/details') {
        $index = array_search('html__something__anotherthing', $vars['theme_hook_suggestions']);
    
        if ($index !== FALSE) {
          unset($vars['theme_hook_suggestions'][$index]);
        }
      } 
    }
    

    This method is probably best as an example only, as you'll have to remember to add more exceptions to the code as you add new pages under /something/anotherthing.

Both examples can also be implemented in a theme if that's preferable.

  • Yep this is the way to go if you want to have templates based on path alias. – cchen Jan 23 '18 at 20:55
  • I definitely want something I won't have to edit for future sub-pages but I think this will get me in the right direction. Will play around with it and accept this when I get it working. – Anonymous Man Jan 23 '18 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.