@@ -36,9 +36,6 @@ class Template
3636 /** @var string */
3737 private const NAME_SEPARATOR = ':: ' ;
3838
39- /** @var string */
40- private const CONTENT_SECTION_KEY = 'content ' ;
41-
4239 protected Renderer $ engine ;
4340
4441 protected string $ filePath ;
@@ -47,6 +44,8 @@ class Template
4744
4845 private Sections $ sections ;
4946
47+ private string $ childContent = '' ;
48+
5049 public function __construct (
5150 string $ name ,
5251 Renderer $ engine ,
@@ -75,7 +74,12 @@ public function render(array $data = []): string
7574 $ this ->withData ($ data );
7675 $ data = $ this ->data ;
7776 $ file = $ this ->getPath ();
78- $ context = new RenderContext ([$ this , 'fetch ' ], $ data , $ this ->sections );
77+ $ context = new RenderContext (
78+ [$ this , 'fetch ' ],
79+ $ data ,
80+ $ this ->sections ,
81+ $ this ->childContent
82+ );
7983 $ content = $ this ->buffer ((
8084 fn () => extract ($ data , EXTR_SKIP ) & include $ file
8185 )->bindTo ($ context ));
@@ -84,7 +88,7 @@ public function render(array $data = []): string
8488
8589 if (! empty ($ parentTpl )) {
8690 $ parent = $ this ->engine ->createTemplate ($ parentTpl , $ this ->sections );
87- $ parent ->getSections ()-> add ( self :: CONTENT_SECTION_KEY , $ content );
91+ $ parent ->setChildContent ( $ content );
8892 $ content = $ parent ->render ($ context ->getParentData ());
8993 }
9094
@@ -137,6 +141,13 @@ public function getSections(): Sections
137141 return $ this ->sections ;
138142 }
139143
144+ public function setChildContent (string $ content ): self
145+ {
146+ $ this ->childContent = $ content ;
147+
148+ return $ this ;
149+ }
150+
140151 private function setFilePathFromName (string $ name ): self
141152 {
142153 $ chunks = explode (self ::NAME_SEPARATOR , $ name );
@@ -160,6 +171,13 @@ private function setFilePathFromName(string $name): self
160171 return $ this ;
161172 }
162173
174+ /**
175+ * @param callable $wrap
176+ *
177+ * @return false|string
178+ *
179+ * @throws Throwable
180+ */
163181 private function buffer (callable $ wrap )
164182 {
165183 $ level = ob_get_level ();
0 commit comments