66
77namespace Hostnet \Component \AccessorGenerator \Twig ;
88
9+ use Twig \Attribute \YieldReady ;
910use Twig \Compiler ;
11+ use Twig \Node \CaptureNode ;
1012use Twig \Node \Node ;
1113
1214/**
2527 * the perline block, otherwise a Twig_Node with sub nodes of all
2628 * the nodes between the prefix and/or postfix.
2729 */
30+ #[YieldReady]
2831class PerLineNode extends Node
2932{
3033 /**
@@ -57,19 +60,19 @@ private function compileComplex(Compiler $compiler): void
5760 {
5861 $ prefix = $ this ->getAttribute ('prefix ' );
5962 $ postfix = $ this ->getAttribute ('postfix ' );
60- $ lines = $ this ->getNode ('lines ' );
6163
6264 // We use normal subcompilation, which uses echo so we buffer our output.
63- $ compiler ->write ("ob_start(); \n" );
64- $ compiler ->subcompile ($ lines );
65+ $ compiler ->write ('$lines = ' );
66+ $ compiler ->subcompile (new CaptureNode ($ this ->getNode ('lines ' ), $ this ->getNode ('lines ' )->lineno ));
67+ $ compiler ->raw ("\n" );
6568
6669 $ ltrim_prefix = ltrim ($ prefix ); // Trimmed version for use on first line
6770 $ indent = ! trim ($ prefix ) && ! trim ($ postfix ); // Are we only indenting or also prefixing
6871
6972 // Fetch the content of the lines inside of this block
70- // and itterate over them
73+ // and iterate over them
7174 $ compiler
72- ->write ("\$lines = explode( \"\\n \", ob_get_clean() ); \n" )
75+ ->write ("\$lines = explode( \"\\n \", \$ lines ); \n" )
7376 ->write ("foreach ( \$lines as \$key => \$line) { \n" )
7477 ->indent (1 );
7578
@@ -82,11 +85,11 @@ private function compileComplex(Compiler $compiler): void
8285
8386 // Write out the prefix for this line.
8487 if ($ prefix ) {
85- $ compiler ->write ("echo \$key > 0 ? ' $ prefix' : ' $ ltrim_prefix' ; \n" );
88+ $ compiler ->write ("yield \$key > 0 ? ' $ prefix' : ' $ ltrim_prefix' ; \n" );
8689 }
8790
8891 // Write the line itself
89- $ compiler ->write ("echo \"\$line \"; \n" );
92+ $ compiler ->write ("yield \"\$line \"; \n" );
9093
9194 // Close if statement for empty line check when indenting.
9295 if ($ indent ) {
@@ -97,7 +100,7 @@ private function compileComplex(Compiler $compiler): void
97100
98101 // Write postfix and new line.
99102 $ compiler
100- ->write ("echo \"$ postfix \\n \"; \n" )
103+ ->write ("yield \"$ postfix \\n \"; \n" )
101104 ->outdent (1 )
102105 ->write ("} \n" );
103106 }
0 commit comments