1212namespace Symfony \UX \TwigComponent \Twig ;
1313
1414use Symfony \UX \TwigComponent \BlockStack ;
15+ use Twig \Attribute \YieldReady ;
1516use Twig \Compiler ;
17+ use Twig \Environment ;
1618use Twig \Extension \CoreExtension ;
1719use Twig \Node \Expression \AbstractExpression ;
1820use Twig \Node \Node ;
21+ use Twig \Node \NodeOutputInterface ;
1922
2023/**
2124 * @author Fabien Potencier <[email protected] > 2225 * @author Kevin Bond <[email protected] > 2326 *
2427 * @internal
2528 */
26- final class ComponentNode extends Node
29+ #[YieldReady]
30+ final class ComponentNode extends Node implements NodeOutputInterface
2731{
2832 public function __construct (string $ component , string $ embeddedTemplateName , int $ embeddedTemplateIndex , ?AbstractExpression $ props , bool $ only , int $ lineno , string $ tag )
2933 {
@@ -64,24 +68,25 @@ public function compile(Compiler $compiler): void
6468 ->string ($ this ->getAttribute ('component ' ))
6569 ->raw (', ' )
6670 ->raw ($ twig_to_array )
67- ->raw ('( ' )
68- ;
71+ ->raw ('( ' );
6972 $ this ->writeProps ($ compiler )
7073 ->raw (') ' )
71- ->raw ("); \n" )
72- ;
74+ ->raw ("); \n" );
7375
7476 $ compiler
7577 ->write ('if (null !== $preRendered) { ' )
7678 ->raw ("\n" )
77- ->indent ()
78- ->write ('echo $preRendered; ' )
79- ->raw ("\n" )
79+ ->indent ();
80+ if (method_exists (Environment::class, 'useYield ' )) {
81+ $ compiler ->write ('yield from $preRendered; ' );
82+ } else {
83+ $ compiler ->write ('echo $preRendered; ' );
84+ }
85+ $ compiler ->raw ("\n" )
8086 ->outdent ()
8187 ->write ('} else { ' )
8288 ->raw ("\n" )
83- ->indent ()
84- ;
89+ ->indent ();
8590
8691 /*
8792 * Block 2) Create the component & return render info
@@ -97,17 +102,15 @@ public function compile(Compiler $compiler): void
97102 ->string ($ this ->getAttribute ('component ' ))
98103 ->raw (', ' )
99104 ->raw ($ twig_to_array )
100- ->raw ('( ' )
101- ;
105+ ->raw ('( ' );
102106 $ this ->writeProps ($ compiler )
103107 ->raw ('), ' )
104108 ->raw ($ this ->getAttribute ('only ' ) ? '[] ' : '$context ' )
105109 ->raw (', ' )
106110 ->string (TemplateNameParser::parse ($ this ->getAttribute ('embedded_template ' )))
107111 ->raw (', ' )
108112 ->raw ($ this ->getAttribute ('embedded_index ' ))
109- ->raw ("); \n" )
110- ;
113+ ->raw ("); \n" );
111114 $ compiler
112115 ->write ('$embeddedContext = $preRenderEvent->getVariables(); ' )
113116 ->raw ("\n" )
@@ -121,8 +124,7 @@ public function compile(Compiler $compiler): void
121124 // happens to contain a {% component %} tag. So we don't need to worry
122125 // about trying to allow a specific embedded template to be targeted.
123126 ->write ('$embeddedContext["__parent__"] = $preRenderEvent->getTemplate(); ' )
124- ->raw ("\n" )
125- ;
127+ ->raw ("\n" );
126128
127129 /*
128130 * Block 3) Add & update the block stack
@@ -143,14 +145,17 @@ public function compile(Compiler $compiler): void
143145 ->string ('outerBlocks ' )
144146 ->raw (']->convert($blocks, ' )
145147 ->raw ($ this ->getAttribute ('embedded_index ' ))
146- ->raw ("); \n" )
147- ;
148+ ->raw ("); \n" );
148149
149150 /*
150151 * Block 4) Render the component template
151152 *
152153 * This will actually render the child component template.
153154 */
155+ if (method_exists (Environment::class, 'useYield ' ) && $ compiler ->getEnvironment ()->useYield ()) {
156+ $ compiler
157+ ->write ('yield from ' );
158+ }
154159 $ compiler
155160 ->write ('$this->loadTemplate( ' )
156161 ->string ($ this ->getAttribute ('embedded_template ' ))
@@ -160,10 +165,16 @@ public function compile(Compiler $compiler): void
160165 ->repr ($ this ->getTemplateLine ())
161166 ->raw (', ' )
162167 ->string ($ this ->getAttribute ('embedded_index ' ))
163- ->raw (') ' )
164- ->raw ('->display($embeddedContext, $embeddedBlocks); ' )
165- ->raw ("\n" )
166- ;
168+ ->raw (') ' );
169+
170+ if (method_exists (Environment::class, 'useYield ' ) && $ compiler ->getEnvironment ()->useYield ()) {
171+ $ compiler ->raw ('->unwrap()->yield( ' );
172+ } else {
173+ $ compiler ->raw ('->display( ' );
174+ }
175+ $ compiler
176+ ->raw ('$embeddedContext, $embeddedBlocks ' )
177+ ->raw ("); \n" );
167178
168179 $ compiler ->write ('$this->extensions[ ' )
169180 ->string (ComponentExtension::class)
0 commit comments