Loop php array in smarty file for dynamic html


I want to loop array in smarty to create dynamic href and link.

Below is the array I am passing to smarty file and looping using foreach.

0,1,2 elements of array completed one href and link and end with hr tag.

I want to make 3,4,5 then hr tag then 6,7,8 like this


[0] => Title1
[1] => ImagePath1
[2] => ImageURL1

[3] => Title2
[4] => ImagePath2
[5] => ImageURL2

[6] => Title3
[7] => ImagePath2
[8] => ImageURL3


{foreach from=$COLLECTION key=k item=v}
< a href=$COLLECTION.3 id=“image_url” target=“_blank” style=“padding-top:10px;align=center” >
< img id=“image_src” src=$COLLECTION.1 / >$COLLECTION.1< / a>
< h r / >

Any idea


I’d do something like:

$el = 0;
while ($el < count($COLLECTION)) { 
  $title = $COLLECTION[$el];
  $path = $COLLECTION[$el+1];
  $url = $COLLECTION[$el+2];
  // output the html code using the variables above
  echo ... whatever ... ;
  $el += 3;


I want to loop using smarty foreach or section to dynamic html output.

It will look something like below

ImageHERE1 Title1
ImageHERE2 Title2
ImageHERE3 Title3

Ah, sorry, no idea. I did see you said “smarty”, had no idea what you were talking about, so just went through in PHP instead.

ETA - it seems you could use {section} instead of {foreach} in smarty. That seems to have a step parameter at least. http://www.smarty.net/docsv2/en/language.function.section.tpl

Or could you change the layout of the array at all, to make it easier to access in either method?

I’d use a chunked array with two nested loops using the @first or @last flag to suppress the overflowing <hr>.


I want to get output same as below php code using smarty. I will pass array in smarty template file.

I am able to get require output using php code but need to get same output using smarty code in smarty template file.

$stack3 = array_chunk($stack, 3);

			$title = $stack3[$i][0];
			$src = $stack3[$i][1];
			$href = $stack3[$i][2];

			$value = "<a href='{$href}' id='image_url' target='_blank' style='padding-top:10px;align=center'>
		<img id='image_src' src='{$src}'' />$title</a><hr/>";

Any idea?


array_chunk() only makes sense with a nested loop. so your code would do something completely different than you expect.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.