Open source software security

Theming Views in Drupal 6

11 June 2010
Views can be really powerful tools for creating lists or aggregations of node content already on your site. There are several powerful output formats for views including lists and sortable tables. However, if you want to move beyond these displays or add additional HTML or functionality to your view you may choose to apply a theme to it. In the same way that you can theme page, block, and node content you can also theme your views. All templates are applied to output based on their filename. So the page.tpl.php file is applied to all pages. If you had a content type called 'foo' you could create a node template for that type by copying the node.tpl.php page in your theme to a new file called node-foo.tpl.php. Then you could add custom PHP or HTML to that template so that when users viewed content of the type 'foo' they would see output differently from other content types. If you're going to theme a view you can use a number of template filenames (and options). To get a sense of what templates are applicable to your view (and which are being used) go to your view at ?q=admin/build/views/edit/[your_viewname], or Administer -> Site building -> Views then click 'Edit'. Select the view you're working with (default, page, block, etc.) from the vertical tab on the left. Next, under 'Basic settings' click the 'Information' link next to 'Theme' (at the very bottom). This will bring up a list of all the templates that reference your view. Some of them exist, others do not. The one in bold is the one currently in use to theme your view. You can look through the list and add a new template with any of the names offered. To view the default content of new templates click the hyperlinked name at the left (for instance 'Display output'). After you create a new template file be sure to click the 'Rescan template files' button at the bottom. This will research your filesystem for the new template, which, if found, will show up bold in the list. Once you're done with your changes be sure to click the 'Save' button! If you create a new template on the filesystem you have to be sure to click the 'Rescan template files' button then the 'Save' button to save the view or the view will continue to use the default file for output!