03rd Aug 2006

Custom Smarty Block Function: Loop

For those who haven’t heard of the Smarty Template Engine for PHP, it’s something you should look into. Of course, I fully agree with it’s designers who state that the system is designed for larger scale applications instead of five page websites.

One of the best features (apart from useful documentation) is the ability to include custom functions to the system. One of the features I’ve been missing is a Loop function similar to PHP’s for loop. So I’ve created one:

<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File:     block.loop.php
* Type:     block function
* Name:     loop
* Purpose:  loops over a piece of template content x amount of times
* -------------------------------------------------------------
*/
function smarty_block_loop($params, $content, &$smarty) {
  //check if the block has data
  if (is_null($content)) {
    return;
  }
  //check if the start parameter is set
  if (strlen($params['start']) == 0 || !is_numeric($params['end'])) {
    $smarty->trigger_error("assign: missing 'start' parameter or is ".
      "not a number");
    return;
  }
  //check if the end parameter is set
  if (strlen($params['end']) == 0 || !is_numeric($params['end'])) {
    $smarty->trigger_error("assign: missing 'end' parameter or is ".
      "not a number");
    return;
  }
  //check if the increment is set. If not, default is +1
  if (empty($params['increment']) || !is_numeric($params['increment'])) {
    $params['increment'] = 1;
  }
 
  //do the loop!
  $data = '';
  //check if we're incrementing or decrementing
  if ($params['end'] > $params['start']) {
    //incrementing, check $increment value is positive
    if ($params['increment'] <= 0) {
      $smarty->trigger_error("assign: increment cannot be negative or ".
        "zero when 'start' is less than 'end'");
      return;
    }
    for ($x=$params['start'];$x < $params['end'];$x=$x+$params['increment']) {
      $data .= $content;
    }
  } else {
    //decrementing, check $increment value is negative
    if ($params['increment'] <= 0) {
      $smarty->trigger_error("assign: increment cannot be positive or ".
        "zero when 'start' is greater than 'end'");
      return;
    }
    for ($x=$params['start'];$x > $params['end'];$x=$x+$params['increment']) {
      $data .= $content;
    }
  }
  //return the data
  return $data;
} //end smarty_block_loop
?>

Download this code: block.loop.php

To use this in your Smarty engine, save this file to the plugins directory. It will automatically be available to you in your scripts. Here’s an example of it’s use:

<p>Smarty loop block function example</p>
<p>Assume that $blocks is defined as 10</p>
{loop start=0 end=$blocks increment=2}
  <div>
    Hello World
  </div>
{/loop}
<p>There should be 5 divs displayed.</p>

Download this code: block.loop.example.html

The increment value is not always required. It defaults to 1 if not specified. Also, this loop code works in reverse. Specifying a start value greater than the end value will cause the loop to decrement. Although, the increment value is required in such case and must be a negative value.

Technorati: php, smarty

Leave a Reply