diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index e8cf5f6f206..a58e1e2c9ca 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -1888,30 +1888,22 @@ public static function checkArrowInSandbox(Environment $env, $arrow, $thing, $ty */ public static function captureOutput(iterable $body): string { - $output = ''; $level = ob_get_level(); ob_start(); try { foreach ($body as $data) { - if (ob_get_length()) { - $output .= ob_get_clean(); - ob_start(); - } - - $output .= $data; - } - - if (ob_get_length()) { - $output .= ob_get_clean(); + echo $data; } - } finally { + } catch (\Throwable $e) { while (ob_get_level() > $level) { ob_end_clean(); } + + throw $e; } - return $output; + return ob_get_clean(); } /**