lots of code take some code, share some code!

Simple Modular Arithmetic with Smarty Template Engine



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.

One Response 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 [...]

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: