0

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:

<div>
   item1-field1
   item2-field1
   item3-field1
</div>
<div>
   item1-field2
   item2-field2
   item3-field2
</div>
<div>
   item1-field3
   item2-field3
   item3-field3
</div>

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.

0

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. enter image description hereenter image description here

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. enter image description here 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.

<?php
dpm($rows); // If you have devel module enabled see your rows;
?>

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.

  • I don't have the devel module and I think $rows doesn't work for the compelxity I need. I'll of course try it but at the moment I'm more in favor with just writing a module and doing my own querying. Thanks for the very nice answer, I'll come back when I tried it. – xotix Jun 17 '17 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.