0

My "Article" node type has a field image. I can print this field in a node template with

<?php print render ($content['field_image']); ?>

I have a simple View which lists Articles here. The default Views template /modules/views/theme/views-view.tpl.php prints the content with <?php print $rows; ?> (line 58 here).

I want to use my own Views template which will specify the various elements of the node ie print title, print image, print body etc instead of <?php print $rows; ?>

I don't think...

<?php print render ($content['field_image']); ?>

...will work in a Views template so what syntax do I need?

In case it helps I have the devel / krumo output on the page.

I am aiming to use the Views template to control the layout of the Articles in the View, for example I might want the View to look like this:

enter image description here

So I think the default Views template's <?php print $rows; ?> is no good for this and I need to isolate the various elements of the View's output (image field etc)

Thanks...

  • Give a bit more context - why you don't want to just output all those fields through Views? – Aram Boyajyan Apr 15 '15 at 12:16
  • @Topsitemakers I am trying to control the layout of the View in the template file because trying to do it in the Views UI has not worked. My question here is meant to be about a simple example View, which I think would be more likely to get replies here than if I ask a question about doing this with my actual View in the Views UI which would be a more difficult / niche question. Is that what you mean by context? – Juc1 Apr 15 '15 at 12:46
1

You not using the correct Views template, views-view.tpl.php is the base template. You need to use views-view-field.tpl.php but you need to use template overrides in your theme.

Firstly, copy views-view-field.tpl.php from the Views module into your theme, make a copy of views-view-field.tpl.php calling it views-view-field--field_image.tpl.php and make your adjustments, remember to clear your template cache so Drupal picks up these files.

  • @ Colin thanks - I have added a final sentence above to clarify that I want to control the layout of the fields not their content - does that affect your answer? – Juc1 Apr 15 '15 at 13:07
  • A little, instead of views-view-field.tpl.php use views-view-fields.tpl.php instead. The file will contain the $fields variable: an array of $field objects so you should be able to access $fields['field_image'], see @Ravi's link for more details. – Colin Shipton Apr 15 '15 at 13:17
1

you should read this for more info about views templates.

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.