Different templates for a content type inside and outside a view

274 Views Asked by At

I have prepared two different templates for Article content type nodes. I would like to use one of the templates in the front page (and show them inside a View, as an unformatted list), and the other template in the rest of the site (outside a View).

I can just define node--article.tpl.php for all the articles, but I have no idea to define another one for the some nodes when they are part of a view (or inside the front page, I don't matter what to use to differentiate both).

2

There are 2 best solutions below

0
thelawnmowerman On BEST ANSWER

I finally learnt a lot more about Drupal 7 and the proper way to do it is (without any code, just using proper names for template files):

  • Using node--article.tpl.php as the template name for the detailed view of the article.
  • Using node--view--[name-of-the-view].tpl.php as the template name for the alternative view for the same articles when they are inside a name-of-the-view view. No matters what kind of nodes are inflated inside the view, this template is taken before the general one.
1
c1u31355 On

I know this is an old question, but as it's not answered and I found it while searching for the same problem, I wanted to give a quick solution.

Since drupal 7 templates are php, you could just quick and dirty check if there is a "parent" view and separate your two templates by an if condition.

<?php
function is_current_node_inside_view() {
  return (isset($view) && is_object($view)) ? true : false;
}

if ( is_current_node_inside_view() ) {
  ?>
    <!-- unformatted list template goes here -->
  <?php
} else {
  ?>
    <!-- second template goes here -->
  <?php
}
?>

I think Drupal best practice would be to use view modes https://drupal.stackexchange.com/questions/37446/how-to-add-additional-displayview-mode-for-node