0

I have a list of pages with url

?q=value/abc/1
?q=value/abc/2
?q=value/abc/3
?q=value/abc/4

for which i require a specific template.I have written a template_preprocess_page function,but it does not call the required tpl file.But if i set the test variable as $test = array("value/abc/1"); the template file is being used.Could someone tell me what i am doing wrong..??

function template_preprocess_page(&$var){  
 $test = array("value/abc/%"); 
        if(in_array($_GET['q'], $test)){
            $var['template_file'] = 'testing';
        }
}
  • 1
    You've tagged this as Drupal 6 and 7...which are you actually using? And I'd be interested to know where you've seen an example that uses template_file I've never seen that used before – Clive Jun 8 '13 at 12:53
  • drupal.org/node/223440, – Outlooker Jun 9 '13 at 3:00
  • So I can guess you're using Drupal 6 from that? Read the examples again, you have typos and small errors in your code that don't match up with what you've tried to copy – Clive Jun 9 '13 at 10:22
  • tanx for joinin in @Clive :) – Outlooker Jun 9 '13 at 16:00
1

Assuming D6 then, in template.php:

/**
 * Implements hook_preprocess_page
 */
function THEMENAME_preprocess_page(&$variables){  
 if (strpos($_GET['q'], 'value/abc') === 0) {
   $variables['template_file'] = 'testing';
 }
}

Clear theme registry, or all caches at admin > config > performance to apply.

More info:

There are two special variables that these hooks can set: 'template_file' and 'template_files'. These will be merged together to form a list of 'suggested' alternate template files to use, in reverse order of priority. template_file will always be a higher priority than items in template_files. theme() will then look for these files, one at a time, and use the first one that exists. https://api.drupal.org/api/drupal/includes%21theme.inc/function/theme/6

  • That's not right. Have a look at the docs the OP linked to, you have the same typo and error that I mentioned in the comments (although you did fix up the path-test nicely) – Clive Jun 9 '13 at 10:39
  • Hi Clive, You can use template_file to add a single suggestion, rather than an array in template_files. ( more info added on that ) I adjusted the function name from template_preprocess_page. Can you be more specific if other error(s) there? – David Thomas Jun 9 '13 at 10:42
  • Oh sorry I didn't realise that, my bad :) those are the only 2 'errors' I was talking about, I was trying to encourage the OP to double check his code against the examples as it didn't match up. Didn't realise it didn't need to! – Clive Jun 9 '13 at 10:46
  • 1
    Heh, no worries, template_file is a curious feature, D7 has a similar feature in theme_hook_suggestion vs theme_hook_suggestions Cheers. – David Thomas Jun 9 '13 at 10:53
  • tanx for the info @DavidThomas.. :) – Outlooker Jun 9 '13 at 15:59
0

I believe what you want is to set up five content-type templates. You can read more about this here and in this tutorial. Essentially if you wish to use a template, you need to create a content-type for it. Then you can use your content-type machine name (let's say your content type's machine name is "foo" then your template PHP file will be titled "page--foo.tpl.php"

You have to then add this to the top of your template.php in your main theme:

function YourTheme_process_page(&$variables) {
      $variables['theme_hook_suggestions'][] = 'page__'. $variables['node']->type;
}

Then you can add a content page for each content type and give it the path you need (ie value/abc/1 or value/abc/2 etc).

If the processing you do with your URL value is minimal, then you can also just retrieve the value through the PHP $_GET function and then use it in your PHP processing as needed (after sanitizing the variable of course).

  • I think we're leaning towards the OP using Drupal 6 (although for some reason he seems reluctant to share that information). This is a good answer for D7 but won't work for D6 – Clive Jun 9 '13 at 10:40

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.