@@ -109,6 +109,7 @@ class View implements RendererInterface
109109 */
110110 protected $ layout ;
111111
112+
112113 /**
113114 * Holds the sections and their data.
114115 *
@@ -121,9 +122,18 @@ class View implements RendererInterface
121122 * if any.
122123 *
123124 * @var string|null
125+ * @deprecated
124126 */
125127 protected $ currentSection ;
126128
129+ /**
130+ * The name of the current section being rendered,
131+ * if any.
132+ *
133+ * @var array<string>
134+ */
135+ protected $ sectionStack = [];
136+
127137 /**
128138 * Constructor
129139 *
@@ -227,7 +237,7 @@ public function render(string $view, array $options = null, bool $saveData = nul
227237 // When using layouts, the data has already been stored
228238 // in $this->sections, and no other valid output
229239 // is allowed in $output so we'll overwrite it.
230- if (! is_null ($ this ->layout ) && empty ( $ this ->currentSection ) )
240+ if (! is_null ($ this ->layout ) && $ this ->sectionStack === [] )
231241 {
232242 $ layoutView = $ this ->layout ;
233243 $ this ->layout = null ;
@@ -402,35 +412,44 @@ public function extend(string $layout)
402412 /**
403413 * Starts holds content for a section within the layout.
404414 *
405- * @param string $name
415+ * @param string $name Section name
416+ *
417+ * @return void
418+ *
406419 */
407420 public function section (string $ name )
408421 {
422+ //Saved to prevent BC.
409423 $ this ->currentSection = $ name ;
424+ $ this ->sectionStack [] = $ name ;
410425
411426 ob_start ();
412427 }
413428
414429 /**
430+ * Captures the last section
431+ *
432+ * @return void
415433 * @throws RuntimeException
416434 */
417435 public function endSection ()
418436 {
419437 $ contents = ob_get_clean ();
420438
421- if (empty ( $ this ->currentSection ) )
439+ if ($ this ->sectionStack === [] )
422440 {
423441 throw new RuntimeException ('View themes, no current section. ' );
424442 }
425443
444+ $ section = array_pop ($ this ->sectionStack );
445+
426446 // Ensure an array exists so we can store multiple entries for this.
427- if (! array_key_exists ($ this -> currentSection , $ this ->sections ))
447+ if (! array_key_exists ($ section , $ this ->sections ))
428448 {
429- $ this ->sections [$ this -> currentSection ] = [];
449+ $ this ->sections [$ section ] = [];
430450 }
431- $ this ->sections [$ this ->currentSection ][] = $ contents ;
432451
433- $ this ->currentSection = null ;
452+ $ this ->sections [ $ section ][] = $ contents ;
434453 }
435454
436455 /**
0 commit comments