forked from FluidTYPO3/vhs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractLoopViewHelper.php
More file actions
79 lines (70 loc) · 2.18 KB
/
AbstractLoopViewHelper.php
File metadata and controls
79 lines (70 loc) · 2.18 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
namespace FluidTYPO3\Vhs\ViewHelpers\Iterator;
/*
* This file is part of the FluidTYPO3/Vhs project under GPLv2 or later.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use FluidTYPO3\Vhs\Core\ViewHelper\AbstractViewHelper;
/**
* Abstract class with basic functionality for loop view helpers.
*/
abstract class AbstractLoopViewHelper extends AbstractViewHelper
{
/**
* @var boolean
*/
protected $escapeChildren = false;
/**
* @var boolean
*/
protected $escapeOutput = false;
public function initializeArguments(): void
{
$this->registerArgument('iteration', 'string', 'Variable name to insert result into, suppresses output');
}
/**
* @return string
*/
protected static function renderIteration(
int $i,
int $from,
int $to,
int $step,
?string $iterationArgument,
RenderingContextInterface $renderingContext,
\Closure $renderChildrenClosure
) {
if (!empty($iterationArgument)) {
$variableProvider = $renderingContext->getVariableProvider();
$cycle = (int) (($i - $from) / $step) + 1;
$iteration = [
'index' => $i,
'cycle' => $cycle,
'isOdd' => 0 === $cycle % 2,
'isEven' => 0 === $cycle % 2,
'isFirst' => $i === $from,
'isLast' => static::isLast($i, $from, $to, $step)
];
$variableProvider->add($iterationArgument, $iteration);
$content = $renderChildrenClosure();
$variableProvider->remove($iterationArgument);
} else {
$content = $renderChildrenClosure();
}
return $content;
}
protected static function isLast(int $i, int $from, int $to, int $step): bool
{
if ($from === $to) {
$isLast = true;
} elseif ($from < $to) {
$isLast = ($i + $step > $to);
} else {
$isLast = ($i + $step < $to);
}
return $isLast;
}
}