How to create template for a view in Drupal

Creating a template for a new Drupal view created by the Views module is straight forward.

There are two options available:

  1. Creating a template for each node that is listed by the view.
  2. Creating a template for each field that is listed by the view.

Creating a template for each node listed

In the edit View screen, change the Row style type to node.

Drupal will generate the HTML by calling node_view() function which is using the standard node.tpl.php template.

To override Drupal’s default behavior, we need to create a node-view-viewname.tpl.php template file which will be called each time a node is being rendered by the view you defined. If your view is called Articles, you need to name the file: node-view-articles.tpl.php.

Make sure you put the template file in your theme’s directory.

Please note that setting the Row style to node will cause DrupalĀ  to load the entire node from the database which may have a performance impact on the web site.

Creating a template for each field listed

In the edit View screen, change the Row style type to fields.

Create views-view-field–viewnamefieldname.tpl.php in your theme’s directory for each field that will be listed by the view you defined. Make sure you set viewname to the view’s name and fieldname to the field’s name.

You can view the file names for each field by clicking on “Theme information” link on the edit view screen.

For additional information check out the Views module help pages.

After creating the template files, make sure you cleared the theme registry by clicking on “Clear cached data” button in Admin > Site Configuration > Performance screen.

2 thoughts on “How to create template for a view in Drupal

  1. Web Solution

    Thanks for sharing this information, view is very useful for us, most of the time by using various parameters the views can be created and showcase the related topic in Drupal

    Reply

Leave a Reply to Web Solution Cancel reply

Your email address will not be published. Required fields are marked *