How to quickly theme a view?
Asked Answered
S

10

86

I've defined a view with the CCK and View 2 modules. I would like to quickly define a template specific to this view. Is there any tutorial or information on this? What are the files I need to modify?


Here are my findings: (Edited)

In fact, there are two ways to theme a view: the "field" way and the "node" way. In "edit View", you can choose "Row style: Node", or "Row style: Fields".

  • with the "Node" way, you can create a node-contentname.tpl.php which will be called for each node in the view. You'll have access to your cck field values with $field_name[0]['value']. (edit2) You can use node-view-viewname.tpl.php which will be only called for each node displayed from this view.
  • with the "Field" way, you add a views-view-field--viewname--field-name-value.tpl.php for each field you want to theme individually.

Thanks to previous responses, I've used the following tools :

  • In the 'Basic Settings' block, the 'Theme: Information' to see all the different templates you can modify.
  • The Devel module's "Theme developer" to quickly find the field variable names.
  • View 2 documentation, especially the "Using Theme" page.
Sexagesima answered 16/9, 2008 at 22:2 Comment(3)
it is views-view-fields--viewname--field-name-value.tpl.php not views-view-field--viewname--field-name-value.tpl.php you are missing an "s" on the first fieldPotto
Which folder do you add this in?Outsole
If you did a search on views documentation page, you would have found all these information!Decile
S
52

In fact there are two ways to theme a view : the "field" way and the "node" way. In "edit View", you can choose "Row style: Node", or "Row style: Fields".

  • with the "Node" way, you can create a node-contentname.tpl.php wich will be called for each node in the view. You'll have access to your cck field values with $field_name[0]['value']
  • with the "Field" way, you add a views-view-field--viewname--field-name-value.tpl.php for each field you want to theme individually.

Thanks to previous responses, I've used the following tools :

  • In the 'Basic Settings' block, the 'Theme: Information' to see all the different templates you can modify.
  • The Devel module's "Theme developer" to quickly find the field variable names.
  • View 2 documentation, especially the "Using Theme" page.
Sexagesima answered 16/9, 2008 at 22:55 Comment(2)
Please note that in the first way, the "node" way, the template you create will probably (depends on theme settings) used for the node itslef (displayed even outside the node).Obolus
Please note that the links in this answer are now 404.Lottie
F
22

A quick way to find the template files you can create and modify for a view in Views 2.0 is to:

  1. Edit the view
  2. Select the style (e.g. page, block, default)
  3. In the 'Basic Settings' block click on 'Theme: Information' to see all the different templates you can modify.
Fiora answered 16/9, 2008 at 23:27 Comment(0)
B
16

The Devel module's "Theme developer" feature is handy for seeing what template files Drupal is looking for when it goes to theme something. See the screenshot on that page for an example.

Battalion answered 17/9, 2008 at 0:20 Comment(0)
L
10

You should also check out Semantic Views. For simple Views theming, it is really handy.

Limousin answered 28/7, 2010 at 4:12 Comment(0)
O
7

One tip:

You'll likely have a number of views which require similar formatting. Creating templates for each of these views and copying them creates a nightmare of code branching - if you're asked to change the whole look and feel of the site (implying changing the display of each of these views formatted in this particular way), you have to go back and edit each of these separately.

Instead of using the views interface to select new templates for views, I sometimes simply insert some code branching into a single views file. E.g. for one site in views-view-fields.tpl.php I have:

if($view->name == 'articleList' || $view->name == 'frontList' 
|| $view->name == 'archiveList') {
/* field formatting code */
} else {
/* the default code running here */
}

This then modifies the fields in the way I want only for this family of Views = articleList, frontList and archiveList - and for other views using this template runs the code one normally finds in this template. If the client asks, "Hey, could you make those pages showing the archives & that list on the front page to look more like ( ... )", it's simply a matter of my opening & editing this one file, instead of three different files. Maintenance becomes much more quick & friendly.

Operator answered 24/3, 2011 at 14:14 Comment(0)
G
6

for me block-views-myViewName-myBlockId.tpl.php works

Geniegenii answered 21/10, 2009 at 0:50 Comment(0)
C
4

My shortcut option.

  1. Go to theme.inc file in YOUR_MODULE_DIR/views/theme/ folder.

  2. In the _views_theme_functions function print the $themes variable or put a breakpoint on the last line of the function to see the content of the variable.

Just convert views_view to views-view and __ to -- and add your template extension to get desired file name.

For example if an element of the $themes array is views_view__test_view__block (where test_view is the name of your view) then the name of the template file would be views-view--test_view--block.tpl.php.

Crenelation answered 8/8, 2009 at 10:22 Comment(0)
M
2

In my opinion the simplest way to decide which template file to use for theming the views is : 1) Click on admin/build/views/edit/ViewName -> Basic Settings -> Theme

Clicking this would list all the possible template files. Highlighted (File names in Bold) files indicate which template file is being used to do theme what part of the view. After incorporating the required changes in the relevant view template file RESCAN .. now you should be able to see the changed template file highlighted .

Magnetics answered 21/2, 2010 at 16:48 Comment(0)
O
1

If you want to do quick Drupal development with a lot of drag-and-drop, the Display Suite module def. is a something you should use: http://drupal.org/project/ds

Odetteodeum answered 1/9, 2011 at 10:11 Comment(0)
S
0

According to me there are two ways to do it:

Programatic Way:

  1. Go to edit view.
  2. Select page/block style.
  3. Go to 'Basic Settings' and click on 'Theme: Information' to see all the different templates you can modify.
  4. Add the html you want to theme and print the variables of the view wherever needed

Configuration Update: Using the Display suiteThe Display suite provides us an option to place your labels inline or above and add even to hide them. Custom classes to each of the view's elements can be added too. Advanced options include:

  • Exportables
  • Add your own custom fields in the backend or in your code
  • Add custom layouts in your theme (D7 only)
  • Change labels, add styles or override field settings (semantic fields).
  • Full integration with Views and Panels
  • Extend the power of your layouts by installing Field Group
  • Optimal performance with Object cache (D6) or Entity cache (D7) integration
Stupa answered 24/1, 2017 at 4:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.