How to access fields in a view template so, I really got stuck. I did a lot of discussion in the IRC and reading but I just fail at it. It should be very easy since it's very simple.

I have a content-type "foo" and I use a view (as a block) to query them from the DB. Now, I'd like to create HTML in one big template. Currently, I created a node template for that content-type and a view template but this doesn't allow to build proper html, instead it forces me to use rather ugly html since it just passes the html created in the node template to my view template.

What I need is to mix the HTML. E.g. if my content-type has the fields "field1, field2, field3" and 3 items, then I'd like to create a html like this:

&lt;div&gt;
   item1-field1
   item2-field1
   item3-field1
&lt;/div&gt;
&lt;div&gt;
   item1-field2
   item2-field2
   item3-field2
&lt;/div&gt;
&lt;div&gt;
   item1-field3
   item2-field3
   item3-field3
&lt;/div&gt;


so how can I do that? How can I access all items and their fields within a view template? I'd like to have full control over the rendered html of the block. I'm on D7.
There are a couple ways you can do this. The easiest way and probably the way you will end up doing it is in your view you need to change the format to a table and then output as fields. It sounds like you are outputting a rendered entity instead of the fields. Then under theme information you will see the available templates that you can overwrite.


The one you will want is under Style. Notice the naming convention. For a view named test and a block the view would be views-view-table--test--block-1.tpl.php.

Create this template and put it into your theme folder and clear your cache. Inside that template you have access to the $rows variable that will contain all of your output.

&lt;?php
dpm($rows); // If you have devel module enabled see your rows;
?&gt;


This should then allow you to style your HTML as needed.

I would advise against this unless you have a special need. A better way to do this would be to use Display Suite in which you can create your own view mode and manage the fields like that. I wont go into detail on how to do that but research would be good to see if it is a good way to go for you.