protected function resolveLoops($template, $variables): string
    {
        return preg_replace_callback(
            '/\{%\s*foreach(?<index>-[\d]+-[\d]+-)\s+' . self::REGEX_VARIABLE . '\s+as\s+(?<scopeVar>[a-z0-9]+)\s*%\}' .
                '(?<body>.+)' .
            '\{%\s*endforeach\k<index>\s*%\}/si',
            function (array $matches) use ($variables): string {
                $output = '';

                foreach ($this->getValue($matches, $variables) as $value) {
                    $scope = array_merge($variables, [$matches['scopeVar'] => $value]);

                    $output .= $this->replaceVariablesInTemplate(
                        $this->resolveConditionals(
                            $this->resolveLoops($matches['body'], $scope),
                            $scope
                        ),
                        $scope
                    );
                }
                return $output;
            },
            $template
        );
    }
protected function resolveLoops($template, $variables): string
    {
        return preg_replace_callback(
            '/\{%\s*foreach(?<index>-[\d]+-[\d]+-)\s+' . self::REGEX_VARIABLE . '\s+as\s+(?<scopeVar>[a-z0-9]+)\s*%\}' .
                '(?<body>.+)' .
            '\{%\s*endforeach\k<index>\s*%\}/si',
            function (array $matches) use ($variables): string {
                $output = '';

                foreach ($this->getValue($matches, $variables) as $value) {
                    $scope = array_merge($variables, [$matches['scopeVar'] => $value]);

                    $output .= $this->replaceVariablesInTemplate(
                        $this->resolveConditionals(
                            $this->resolveLoops($matches['body'], $scope),
                            $scope
                        ),
                        $scope
                    );
                }
                return $output;
            },
            $template
        );
    }
protected function resolveLoops ( $ template , $ variables ) : string { return preg_replace_callback ( '/\{%\s*foreach(?<index>-[\d]+-[\d]+-)\s+' . self :: REGEX_VARIABLE . '\s+as\s+(?<scopeVar>[a-z0-9]+)\s*%\}' . '(?<body>.+)' . '\{%\s*endforeach\k<index>\s*%\}/si' , function ( array $ matches ) use ( $ variables ) : string { $ output = '' ; foreach ( $ this -> getValue ( $ matches , $ variables ) as $ value ) { $ scope = array_merge ( $ variables , [ $ matches [ 'scopeVar' ] => $ value ] ) ; $ output .= $ this -> replaceVariablesInTemplate ( $ this -> resolveConditionals ( $ this -> resolveLoops ( $ matches [ 'body' ] , $ scope ) , $ scope ) , $ scope ) ; } return $ output ; } , $ template ) ; }
Resolve loops in a given template section

@param string $template  The template section
@param array  $variables The current variable scope

@return string
@throws UndefinedSymbolException
@throws SyntaxErrorException
Resolve loops in a given template section