@@ -120,9 +120,9 @@ class View implements RendererInterface
120120 * The name of the current section being rendered,
121121 * if any.
122122 *
123- * @var string|null
123+ * @var array< string>
124124 */
125- protected $ currentSection ;
125+ protected $ sectionStack = [] ;
126126
127127 /**
128128 * Constructor
@@ -227,7 +227,7 @@ public function render(string $view, array $options = null, bool $saveData = nul
227227 // When using layouts, the data has already been stored
228228 // in $this->sections, and no other valid output
229229 // is allowed in $output so we'll overwrite it.
230- if (! is_null ($ this ->layout ) && empty ( $ this ->currentSection ) )
230+ if (! is_null ($ this ->layout ) && $ this ->sectionStack === [] )
231231 {
232232 $ layoutView = $ this ->layout ;
233233 $ this ->layout = null ;
@@ -402,35 +402,41 @@ public function extend(string $layout)
402402 /**
403403 * Starts holds content for a section within the layout.
404404 *
405- * @param string $name
405+ * @param string $name Section name
406+ *
407+ * @return void
406408 */
407- public function section (string $ name )
409+ public function section (string $ name ): void
408410 {
409- $ this ->currentSection = $ name ;
411+ $ this ->sectionStack [] = $ name ;
410412
411413 ob_start ();
412414 }
413415
414416 /**
417+ * Captures the last section
418+ *
419+ * @return void
415420 * @throws RuntimeException
416421 */
417- public function endSection ()
422+ public function endSection (): void
418423 {
419424 $ contents = ob_get_clean ();
420425
421- if (empty ( $ this ->currentSection ) )
426+ if ($ this ->sectionStack === [] )
422427 {
423428 throw new RuntimeException ('View themes, no current section. ' );
424429 }
425430
431+ $ section = array_pop ($ this ->sectionStack );
432+
426433 // Ensure an array exists so we can store multiple entries for this.
427- if (! array_key_exists ($ this -> currentSection , $ this ->sections ))
434+ if (! array_key_exists ($ section , $ this ->sections ))
428435 {
429- $ this ->sections [$ this -> currentSection ] = [];
436+ $ this ->sections [$ section ] = [];
430437 }
431- $ this ->sections [$ this ->currentSection ][] = $ contents ;
432438
433- $ this ->currentSection = null ;
439+ $ this ->sections [ $ section ][] = $ contents ;
434440 }
435441
436442 /**
0 commit comments