wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
block.repeat.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: block.repeat.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 
20 /*
21 * Smarty plugin
22 * -------------------------------------------------------------
23 * File: block.repeat.php
24 * Type: block
25 * Name: repeat
26 * Purpose: repeat a template block a given number of times and replace
27 * {literal}{$index}{/literal} by the current index
28 * (NOTE: $index has to be enclosed by literal)
29 * Parameters: count [required] - number of times to repeat
30 * assign [optional] - variable to collect output
31 * startindex [optional] - index value to start from
32 * strformat [optional] - format string to apply on index
33 * separator [optional] - separator to be added inbetween
34 * Usage: {repeat count=$content->getValue("numPages") startindex="1" strformat="%02s" separator=" | "}
35 * ... text to repeat {literal}{$index}{/literal} ...
36 * {/repeat}
37 *
38 * Author: Scott Matthewman <scott@matthewman.net>
39 * Ingo Herwig <ingo@wemove.com> (index, separator enhancement)
40 * -------------------------------------------------------------
41 */
42 function smarty_block_repeat($params, $content, &$smarty)
43 {
44  if (!empty($content))
45  {
46  $intCount = intval($params['count']);
47  if($intCount < 0)
48  {
49  $smarty->trigger_error("block: negative 'count' parameter");
50  return;
51  }
52 
53  $strRepeat = '';
54  for ($i=0; $i<$intCount; $i++)
55  {
56  $index = $i + intval($params['startindex']);
57  if (isset($params['strformat']))
58  $indexStr = sprintf($params['strformat'], $index);
59  else
60  $indexStr = $index;
61 
62  $strRepeat .= str_replace('{$index}', $indexStr, $content);
63 
64  if (isset($params['separator']) && $i<$intCount-1)
65  $strRepeat .= $params['separator'];
66  }
67 
68  if (!empty($params['assign']))
69  $smarty->assign($params['assign'], $strRepeat);
70  else
71  echo $strRepeat;
72  }
73 }
74 ?>
smarty_block_repeat($params, $content, &$smarty)