summaryrefslogtreecommitdiff
path: root/smartyplugins/BlockRepeat.php
blob: 293f2fa92088e82d82eb150f0cd33e8597d1787e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace Bitweaver\Plugins;

use Smarty\BlockHandler\BlockHandlerInterface;
use Smarty\Template;
use Smarty\Exception;

/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * 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>
 * -------------------------------------------------------------
 */
class BlockRepeat implements BlockHandlerInterface {

	public function handle( $params, $content, Template $template, &$repeat): string {
		global $gBitSmarty;
		if( !empty( $content ) ) {
			$intCount = (int) ( $params['count'] );
			if( $intCount < 0 ) {
				throw new Exception(
					"block: negative 'count' parameter", );
			}

			$strRepeat = str_repeat( $content, $intCount );
			if( !empty( $params['assign'] ) ) {
				$gBitSmarty->assign($params['assign'], $strRepeat );
			} else {
				echo $strRepeat;
			}
		}
		return '';
	}

	public function isCacheable(): bool {
		return true;
	}
}