block.repeat.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 
12 /*
13 * Smarty plugin
14 * -------------------------------------------------------------
15 * File: block.repeat.php
16 * Type: block
17 * Name: repeat
18 * Purpose: repeat a template block a given number of times and replace
19 * {literal}{$index}{/literal} by the current index
20 * (NOTE: $index has to be enclosed by literal)
21 * Parameters: count (required) - number of times to repeat
22 * assign (optional) - variable to collect output
23 * startindex (optional) - index value to start from
24 * strformat (optional) - format string to apply on index
25 * separator (optional) - separator to be added inbetween
26 * Usage: {repeat count=$content->getValue("numPages") startindex="1" strformat="%02s" separator=" | "}
27 * ... text to repeat {literal}{$index}{/literal} ...
28 * {/repeat}
29 *
30 * Author: Scott Matthewman <scott@matthewman.net>
31 * Ingo Herwig <ingo@wemove.com> (index, separator enhancement)
32 * -------------------------------------------------------------
33 */
34 function smarty_block_repeat($params, $content, \Smarty_Internal_Template $template, &$repeat) {
35  if (!empty($content)) {
36  $intCount = intval($params['count']);
37  if($intCount < 0) {
38  $template->trigger_error("block: negative 'count' parameter");
39  return;
40  }
41 
42  $strRepeat = '';
43  for ($i=0; $i<$intCount; $i++) {
44  $index = $i + intval($params['startindex']);
45  if (isset($params['strformat'])) {
46  $indexStr = sprintf($params['strformat'], $index);
47  }
48  else {
49  $indexStr = $index;
50  }
51  $strRepeat .= str_replace('{$index}', $indexStr, $content);
52 
53  if (isset($params['separator']) && $i<$intCount-1) {
54  $strRepeat .= $params['separator'];
55  }
56  }
57 
58  if (!empty($params['assign'])) {
59  $template->assign($params['assign'], $strRepeat);
60  }
61  else {
62  echo $strRepeat;
63  }
64  }
65 }
66 ?>