String Repeat

Author: Ken Stanley <smarty@kennethpaul.com>

This plugin gives you the same results as the native PHP function str_repeat does. There

are three attributes to the function: STRING (String to repeat) and COUNT (How many times to repeat) and ASSIGN which allows you to stuff the results into a template variable to use

as many times throughout the template as you wish.

<?php

/*
 * Smarty plugin
 * -------------------------------------------------------------
 * Type:     function
 * Name:     str_repeat
 * Author:   Ken Stanley <smarty@kennethpaul.com>
 * Purpose:  print out a value x amount of times
 * -------------------------------------------------------------
 */
function smarty_function_str_repeat($params, &$smarty)
{
    static $string = array();
    static $count = array();
    static $assign = "";

    extract($params);
    
    if (!empty($assign))
        $smarty->assign($assign, str_repeat($string[0],$count[0]));
    else
        echo str_repeat($string[0],$count[0]);
}
?>

Block Repeat

Author: Scott Matthewman <scott@matthewman.net>

As an alternative to the function above, the following function allows you to repeat a block of a template a given number of times, for example:

{repeat count="3"}
   ... text to repeat ...
{/repeat}

Note that the contents of the repeat block are parsed only once.

<?php
/*
* Smarty plugin
* -------------------------------------------------------------
* File:     block.repeat.php
* Type:     block
* Name:     repeat
* Purpose:  repeat a template block a given number of times
* Parameters: count [required]  - number of times to repeat
*             assign [optional] - variable to collect output
* Author:   Scott Matthewman <scott@matthewman.net>
* -------------------------------------------------------------
*/
function smarty_block_repeat($params, $content, &$smarty)
{
    if (!empty($content)) {
        $intCount = intval($params['count']);
        if($intCount < 0) {
            $smarty->trigger_error("block: negative 'count' parameter");
            return;
        }
        
        $strRepeat = str_repeat($content, $intCount);
        if (!empty($params['assign'])) {
            $smarty->assign($params['assign'], $strRepeat);
        } else {
            echo $strRepeat;
        }
    }
}
?>

Back to SmartyPlugins?.