preload
jQuery - Skinning HTML Select Boxes Simple Smarty Pagination
Mar 24

Something i take for granted in PHP is the use of Modular Arithmetic when display results in a grid like format.

Usually in php, i would do something like this to display three items per row

if ($count % 3 == 0) {

// Add Break / Clear here

}

So i assumed something like this would be available in smarty. After googling for a while i found a couple of solutions to this, however none of them were as simple as the PHP code above, so by playing about with the Smarty Template Engine, i managed to figure out a nice simple way, and here it is.

{assign var="mycount" value="1"} {*Initiate the mycount variable*}
{if $mycount++ % 3 == 0} {*Simular syntax to the above*}
// Add Break / Clear here
{/if}

So, as i found this so useful for displaying products on a current project, i thought it would be something other would really like to know about.

2 Responses to “Simple Modular Arithmetic with Smarty Template Engine”

  1. PHP Coding School Says:

    [...] Simple Modular Arithmetic with Smarty Template Engine By lotsofcode After googling for a while i found a couple of solutions to this, however none of them were as simple as the PHP code above, so by playing about with the Smarty Template Engine, i managed to figure out a nice simple way, and here it is. … Lots of Code - http://www.lotsofcode.com [...]

  2. Brian Says:

    Another method is to utilize the "iteration" property of the loop itself. For example:

    {foreach from=$records item="r" name="rLoop"}
    {if $smarty.foreach.rLoop.iteration % 3 == 0} Break to new row {/if}
    {/foreach}

Leave a Reply

 
 
Please enter the word above into the box below, the word is not case sensitive.
 
If you have trouble reading the word above then click here to load a new word.
Security word: