Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I want to use different node templates if the url is appended with ?template=x

Content type is called image

Default Node template using node--image.tpl.php:

  • blakedesign.co.nz/testsite/node/1

Same node with different template via URL possibly node--image--templateX.tpl.php:

  • blakedesign.co.nz/testsite/node/1?template=2
  • blakedesign.co.nz/testsite/node/1?template=3
  • blakedesign.co.nz/testsite/node/1?template=4

How do i go about doing this?

I have made an example at http://blakedesign.co.nz/testsite/node/1

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

You can create custom template suggestions for node using hook_preprocess_node... In your case it should be below code in your theme template.php file... You can read query arguments using $_GET variable..

function YOURTHEME_preprocess_node(&$variables) {
  if ($variables['type'] == "image" && isset($_GET['template'])) {
    $variables['theme_hook_suggestion'] = 'node__image__template' . $_GET['template'];
  }
}

Your template file should be...

node--image--template1.tpl.php

node--image--template2.tpl.php

node--image--template3.tpl.php etc...
share|improve this answer
1  
Works like a charm. Changed if ($variables['type'] == "image" && $_GET['template']) { to if ($variables['type'] == "image" && isset($_GET['template'])) { –  blakedesign May 9 at 3:18
    
shouldn't it would be better if we use $variables['theme_hook_suggestion'][] –  Human love May 11 at 15:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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