Theming a slider view using templates I am creating theming view using row style theme named as 


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


and here is code for it:

&lt;?php  
$string='&lt;div id="slider"&gt;&lt;ul&gt;';


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

&lt;?php   

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

}
?&gt;
&lt;?php endforeach;  ?&gt;

&lt;?php 
$string.="&lt;/ul&gt;&lt;/div&gt;";

echo $string;
?&gt;


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?
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)