0

I am using Drupal 7 to create a page and a custom template for that page.

I created a page from Content >> Add Content >> Page.

In my theme /template folder I have created a file called page--my-test-page.tpl.php. In that template, I have a test echo which just displays hello world, which isn't displayed.

Where am I going wrong?

1

Theme suggestions like that do not work on node titles, out of the box. You could theoretically add your own suggestions based on node title, but it seems like a very bad idea to me because editors can change titles at will, which will immediately break the correspondance to the templates in your file system.

If you really want to target an individual node, page--node--1.tpl.php should work, where 1 is the node ID of your "My test page" content. But to be honest that is probably not a great idea either, because node IDs can also change very easily, especially between environments, whereas template filenames do not. For example you may create page--node--57.tpl.php because your custom page in question is node 57, but if the editor deletes it and then recreates it, even with the same name and same path, it will suddenly be node 109 and not match.

Ultimately, trying to target an arbitrary specific page with custom HTML is somewhat counter to the entire notion of a CMS. I'm not trying to sound preachy here, just explaining that you are inevitably going to be "swimming uphill" a bit here. The CMS "wants" you to do your templating based on categories and types - news stories vs events and so on - and not on the actual contents of the content, because editors can change all that on a whim.

So, you could for example solve this by writing a custom hook. See for example this example fragment which adds page.tpl suggestions based on content type. It's just PHP, so you're free to do whatever. So you can do:

if ($variables['node']->title == "My custom page")) {
   // the file name will be "page--customoverride.tpl.php".
   $variables['theme_hook_suggestions'][] = 'page__customoverride';
}

and you've got a dedicated page template for that item, but you still haven't really solved your problem, because if the editor changes title you're still back to square one.

You could of course add some extra field to your content types, like a "Use special styling" checkbox, and use that as the criteria to alter the theme suggestions. It's still possible for editors to change, but at least it's a dedicated, clearly labelled thing they won't change accidentally without realising that doing so has associated consequences to the templating.

So, hard to say what the best way to tackle this is. Depending on the scope of your site and how many staff you have, etc, it could be as simple as agreeing "Don't change the title of that page, guys!" Or you may feel obliged to create a bespoke content type purely for this one-off page, or a custom module to control this one-off page, or whatever other more complicated technical solution.

  • I think I follow, what is the best way of doing it then? I come from a WordPress world so have gotten myself really confused – fightstarr20 Mar 13 at 13:20
  • Sorry, disregard that last (now deleted) comment, I didn't read the docs carefully enough. I'll have a think and come back with a better one. – stevekeiretsu Mar 13 at 13:30
  • Comment boxes are too short, so I expanded my original answer. Unfortunately I haven't any wordpress knowledge so I can't draw analogies with how that works – stevekeiretsu Mar 13 at 13:56

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.