How to create a foreach loops in smarty?


The demand for PHP websites is already on rise as more and more people today prefer to have their websites done with PHP scripting language. Even those who are new to the cyberspace and looking for opportunities to come up with their own websites prefer to go for PHP as it is simple and open source and available freely on different websites on internet with codes and scripting language almost similar to that of C, C++ and Java. The other factors that play an important role in the growth of PHP websites are stability and speed. With PHP websites are designed in a simple way that makes it faster and efficient compare to the HTML websites that are now becoming history. As the demand for PHP is growing, other systems and applications related to PHP are also becoming popular and one of them is Smarty.

Smarty is a web template system written in PHP websites by generating web content by the placement of special Smarty tags within a document. These tags are processed and substituted with other code. These tags are like commands for Smarty that are enclosed by template delimiters. These commands can be any variables denoted by a dollar sign ($), functions, logical or loop statements that allows PHP programmers to define custom functions that can be accessed using Smarty tags. Smarty tags are used where a web application or a website has a theme system built into it and where the templates can be changed from theme to theme.

Smarty foreach loops allows you to know how many times they have looped so far which you can use to perform a task every two or more loop iterations. The special Smarty variable .iteration tracks the number of times the loop has looped so far. Smarty foreach loop is used to loop over an associative array as well a numerically-indexed array, unlike section which is for looping over numerically-indexed arrays only. The syntax for foreach loop is much easier than section loop but as a tradeoff it can only be used for a single array. Every foreach loop tag must be paired with a closing foreach loop tag.

Smarty foreach loop requires from and item attributes and the name of the foreach loop can be anything you like made up on numbers, letters and underscores like in other PHP variables. While creating each foreach loop must be nested uniquely from each other. The from attribute will determine the number of times the loop will be executed and foreachelse attribute will execute when there is no value in the from variable.
{foreach from=$carma_video.files.$type key=id item=file name=myloop}

<!-- Code to display the videos with whatever HTML formatting you wish -->

{if ($smarty.foreach.myloop.iteration) % 4== 0}</tr><tr>{/if}

{/foreach}
The above makes Smarty end one table row and start a new table row after every four loop iterations. Though Smarty can be easy for those who are not new to Unix, but those who are using Windows applications might need a little patience to understand the concept.



Posted by James on 2009-10-22 in the category " php "