1

I am creating theming view using row style theme named as

views-view-fields--slider-block.tpl.php

and here is code for it:

<?php  
$string='<div id="slider"><ul>';


foreach($fields['field_slider_image_fid']->handler->view->style_plugin->rendered_fields as $row) : ?>
<?php $i=1; ?>
<?php $i=$i+1;?>

<?php   

if($row['field_image_position_value'] == 'left') {
    $string.="<li>
".$row['field_slider_image_fid']."".$row['field_image_text_value']."</li>";
}
else {
    $string.="<li>".$row['field_image_text_value']."".$row['field_slider_image_fid']."</li>";

}
?>
<?php endforeach;  ?>

<?php 
$string.="</ul></div>";

echo $string;
?>

Now it is working fine when there is exactly one node in the view. But a problem occurs when the view contains more than one node, e.g. if the view contains 5 nodes, this template renders 5 times causing duplication.

Anyone have ideas on how to resolve this?

1

Apart from your template needed some serious code cleanup (to follow the coding standards for PHP Template), I would say your issue lies with the fact that you have overridden the wrong views template.

It seems like your views template is doing the iterations around the nodes itself (from the foreach), and I bet your view is also doing the same (hence the double up).

My suggestions:

  • Read https://drupal.org/coding-standards - thoroughly
  • Also look at the default implementations in the views templates, these will give your excellent examples on how your code should look

As for your problem, either:

  • override a higher level views template (i.e. one that only gets rendered once per view)
  • or alternatively, use the same template, and leave out the foreach loop (as the view is essentially doing this. I would say this is the 'cleaner' option. All the fields you have pulled out in your view will be available to be used without having to access the global $view object.

Consider installing the devel module to help with outputting variables, so you know what is available to be themed (using the dsm() function)

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.