0

I just created a template page--article.tpl.php to apply ton nodes of the [article] type. It works when declared in the template.php file.
My problem is that there is a very slight change compared to the generic page.tpl.php, nothing worth having another file.

I was wondering if I could place the adde part between a condition, just as I could do with if $is_front Something like:

<?php if ($node->type == 'article')
  // render my thingees
?>

I would be glad if someone could tell me.

--- Edit, to answer Kiwimind ---

I did the test with the 'page' content type, so I didn't have to skip my page--article.tpl.php file. I checked that 'page' was the actual machine name and 'nature' is my theme's actual name, it works on other functions.
I wrote in template.php

/**
 * Détection du type de contenu pour affecter le template page
 * https://drupal.stackexchange.com/questions/264550/can-i-target-a-specific-node-type-in-page-tpl-php
 */

function nature_preprocess_page(&$variables, $hook) {
  $variables['is_page'] = false;
  if (isset($variables['node']) && $variables['node']->type == 'page') {
    $variables['is_page'] = true;
  }
}

Then in page.tpl.php

<?php if ($is_page) : ?> 
    <h1>This is a “page” content</h1> 
<?php endif; ?>

Nothing more, nothing less. Do you see any missing (or unnecessary) character, that could trigger a parse error? For I still get a whole blank page when clearing the cache.

2 Answers 2

3

As shown on Variable to check if the page is of some other Content Type, you can drop into template.php something like the following:

function THEMENAME_preprocess_page(&$variables, $hook) {
  $variables['print_extra_header'] = false;
  if (isset($variables['node']) && $variables['node']->type == 'product') {
    $variables['print_extra_header'] = true;
  }
}

then in your template, add in

<?php if ($print_extra_header) : ?> <h1>THIS GETS PRINTED</h1> <?php endif; ?>

So effectively you're checking on the node type in a preprocess page, to pass a custom variable to the template for you to act on.

3
  • I tried and I got a blank page when clearing the cache. I'll carry on with a page--article.tpl.php, nevermind. Thank you anyway, It could have been just the right thing I needed.
    – Mars
    Jul 10, 2018 at 15:12
  • If you paste in the code that you tried, I'm quite happy to have a look. There are a few things in the above example that would need amending for your use case.
    – kiwimind
    Jul 11, 2018 at 14:49
  • I understood what went wrong. I was already using the function XXX_preprocess_page() to target the specific template I was talking about in the very beginning. I copied your code underneath and then had two functions with the same name.
    – Mars
    Jul 24, 2018 at 13:40
0

You can set variable as per your condition on your module file and get this variable from your .tpl file after that set your condition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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