MVH Smarty n00b
Joined: 04 Mar 2011 Posts: 3
|
Posted: Mon Mar 14, 2011 9:31 am Post subject: Plugin "columnSeparator" - split rows to columns |
|
|
Code: | <?php
/**
* Split rows to columns.
* @version 1.0
* @author Mikhail Kharitonov <mvh@list.ru>
*
* Example 1:
* <table>
* {$foo=0}
* {foreach array(1,2,3,4,5,6,7,8,9,10) as $number}
* {columnSeparator size=5 total=$number@total iteration=$number@iteration}
* </table><table>
* {* Code in this block will be executed only when condition is true *}
* {$foo=$foo+1}
* {/columnSeparator}
*
* <tr>{$number}</tr>
* {/foreach}
* </table>
* Foo is {$foo}
*
* Output:
* <table>
* <tr>1</tr>
* <tr>2</tr>
* <tr>3</tr>
* <tr>4</tr>
* <tr>5</tr>
* </table><table>
* <tr>6</tr>
* <tr>7</tr>
* <tr>8</tr>
* <tr>9</tr>
* <tr>10</tr>
* </table>
* Foo is 1
* -----------------------------------------------------------
* Example 2:
* <table>
* {foreach array(1,2,3,4,5,6,7,8,9,10) as $number}
* {columnSeparator size='50%' groupSize=3 total=$number@total iteration=$number@iteration}
* </table><table>
* {/columnSeparator}
*
* <tr>{$number}</tr>
* {/foreach}
* </table>
*
* Output:
* <table>
* <tr>1</tr>
* <tr>2</tr>
* <tr>3</tr>
* <tr>4</tr>
* <tr>5</tr>
* <tr>6</tr>
* </table><table>
* <tr>7</tr>
* <tr>8</tr>
* <tr>9</tr>
* <tr>10</tr>
* </table>
* -----------------------------------------------------------
* Example 3 (nested columns):
* <div>
* <table>
* {foreach array(1,2,3,4,5,6,7,8,9,10) as $number}
* {columnSeparator size=2 total=$number@total iteration=$number@iteration}
* </table>
* {columnSeparator size=6 total=$number@total iteration=$number@iteration}
* </div>
* <div>
* {/columnSeparator}
* <table>
* {/columnSeparator}
*
* <tr>{$number}</tr>
* {/foreach}
* </table>
* </div>
*
* Output:
* <div>
* <table>
* <tr>1</tr>
* <tr>2</tr>
* </table>
* <table>
* <tr>3</tr>
* <tr>4</tr>
* </table>
* <table>
* <tr>5</tr>
* <tr>6</tr>
* </table>
* </div>
* <div>
* <table>
* <tr>7</tr>
* <tr>8</tr>
* </table>
* <table>
* <tr>9</tr>
* <tr>10</tr>
* </table>
* </div>
*
* @param int|string $params[size] Rows in column (integer or percentage, for example "50%")
* @param int $params[groupSize] Minimal unseparated rows group size
* @param int $params[total] Rows count
* @param int $params[iteration] Current iteration (from 1)
*/
function smarty_block_columnSeparator($params, $content, $smarty, &$repeat)
{
if ($repeat)
{
$params['size'] = trim($params['size']);
if (preg_match('/\%$/is', $params['size']))
{
$percentage = (float) $params['size'];
$params['size'] = ceil($params['total'] * $percentage / 100);
}
if (@$params['groupSize'] > 0 && $params['size'] % $params['groupSize'])
$params['size'] += $params['groupSize'] - $params['size'] % $params['groupSize'];
if ($params['size'] > 0)
{
$isBreak = (($params['iteration'] - 1) % $params['size'] == 0);
if ($isBreak && $params['total'] > 1 && $params['iteration'] > 1)
$repeat = true;
else
$repeat = false;
}
else
$repeat = false;
}
else
return $content;
}
// Unit tests
//class smarty_block_columnSeparatorTest extends PHPUnit_Framework_TestCase
//{
// protected $smarty;
// protected function setUp()
// {
// $this->smarty = new Smarty();
// $this->smarty->registerPlugin('block', 'columnSeparator', 'smarty_block_columnSeparator');
// }
//
// function provider()
// {
// return array(
// array(0, 0, '1234567890'),
// array(1, 0, '1|2|3|4|5|6|7|8|9|0'),
// array(2, 0, '12|34|56|78|90'),
// array(3, 0, '123|456|789|0'),
// array(4, 0, '1234|5678|90'),
// array(5, 0, '12345|67890'),
// array(6, 0, '123456|7890'),
// array(7, 0, '1234567|890'),
// array(8, 0, '12345678|90'),
// array(9, 0, '123456789|0'),
// array(10, 0, '1234567890'),
//
// array('0%', 0, '1234567890'),
// array('10%', 0, '1|2|3|4|5|6|7|8|9|0'),
// array('20%', 0, '12|34|56|78|90'),
// array('30%', 0, '123|456|789|0'),
// array('40%', 0, '1234|5678|90'),
// array('50%', 0, '12345|67890'),
// array('60%', 0, '123456|7890'),
// array('70%', 0, '1234567|890'),
// array('80%', 0, '12345678|90'),
// array('90%', 0, '123456789|0'),
// array('100%', 0, '1234567890'),
//
// array(0, 1, '1234567890'),
// array(0, 2, '1234567890'),
// array(0, 3, '1234567890'),
//
// array(2, 1, '12|34|56|78|90'),
// array(2, 2, '12|34|56|78|90'),
// array(2, 3, '123|456|789|0'),
//
// array(3, 1, '123|456|789|0'),
// array(3, 2, '1234|5678|90'),
// array(3, 3, '123|456|789|0'),
//
// array(5, 1, '12345|67890'),
// array(5, 2, '123456|7890'),
// array(5, 3, '123456|7890'),
//
// array(10, 1, '1234567890'),
// array(10, 2, '1234567890'),
// array(10, 3, '1234567890'),
// );
// }
//
// /**
// * @dataProvider provider
// */
// public function testColumnSeparator($size, $groupSize, $expected)
// {
// $tpl = $this->smarty->createTemplate('eval:{strip}
// {foreach array(1,2,3,4,5,6,7,8,9,0) as $item}
// {columnSeparator size="' . $size . '" groupSize="' . $groupSize . '" total=$item@total iteration=$item@iteration}
// |
// {/columnSeparator}
// {$item}
// {/foreach}
// {/strip}');
//
// $this->assertEquals($expected, $this->smarty->fetch($tpl));
// }
//} |
|
|