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...

2
  • Give a bit more context - why you don't want to just output all those fields through Views? Apr 15, 2015 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, 2015 at 12:46

2 Answers 2

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.

2
  • @ 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, 2015 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. Apr 15, 2015 at 13:17
1

you should read this for more info about views templates.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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