I'm working on a custom theme and I have two page templates.

  1. page.tpl.php
  2. page--node--637.php

Template 1 is the default template. Template 2 is identical to template 1, except the layout is slightly modified.

Now, I have some more nodes for which I would like to use template 2 (the slightly modified layout).

The only way I know of to do that is to duplicate the template for each node and modify the node id.

For example:

  1. page.tpl.php
  2. page--node--627.tpl.php
  3. page--node--152.tpl.php
  4. page--node--28.tpl.php
  5. page--node--62.tpl.php
  6. ...

Rather than create a bunch of identical templates that must be maintained, I'd rather make template 2 generic (call it something like page--alt.tpl.php) and then specify which pages should use that template (I'm guessing that would go in my theme's preprocess hook).

Is there a way to do that?


I will look into Panels and Context to see if I can figure it out. This is an inherited project and the ways in which panels and contexts are being utilized isn't clear, especially for someone like me who hasn't really worked with either module.

That's why I was attempting to solve the issue at the theme level.

Here are a few more details that may help:

  • The client isn't worried about being able to configure which layout is used. This is a one-time thing for a certain subset of pages. If configuration is necessary, that functionality will be developed in the future.
  • In particular, we need all pages under a certain path to use this alternate layout. For example, we need to enforce this layout for /about/*.

I'll spend some time learning panels and contexts, but perhaps these details will help inform other solutions?

  • 1
    There are lots of ways of allowing some nodes to have different structures than others, most of which are not handled in the tpl files but in other parts of Drupal. Is there a reason you want to go that route over using tools like Context or Panels? If you don't have a reason, I recommend you look into those modules (there are other tools as well). – acrosman Nov 29 '16 at 16:45
  • 2
    @acrosman a custom solution for this is about 5 lines of code (including function signature and brackets)..adding the enormous weight of panels/context just for that would be bordering on irresponsible! – Clive Nov 29 '16 at 17:08
  • @clive if you're doing it for a small number of pages on one project I mostly agree. If you have lots of cases you need to handle or solving the same problem across several projects I think the benefits of context far outweighs the effort needed to set it up (and I've seen negligible difference in real-world production performance), particularly since Context frees you from dealing with the core block UI. – acrosman Nov 29 '16 at 17:27
  • If this is just one project, the cheapest route is to use a field on a node, check the field value in a preprocess and add template suggestion ('page--layout_foo.tpl.php'). If you only have the one alternate layout, that isn't many lines of code. If you plan to expand on this, you should probably learn Context or consider Panels. – Kevin Nov 29 '16 at 17:57
  • I've updated the question with additional details. – ssteigen Nov 29 '16 at 19:07

Your Answer

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

Browse other questions tagged or ask your own question.