@@ -97,7 +97,7 @@ public function setLayout($layout)
9797 if (!is_file ($ layoutPath )) {
9898 throw new \RuntimeException ("Layout template ` $ layout` does not exist " );
9999 }
100- $ this ->layout = $ layoutPath ;
100+ $ this ->layout = $ layout ;
101101 }
102102 }
103103
@@ -182,6 +182,33 @@ public function setTemplatePath($templatePath)
182182 * @throws \RuntimeException
183183 */
184184 public function fetch ($ template , array $ data = [], $ useLayout = false ) {
185+
186+ $ output = $ this ->fetchTemplate ($ template , $ data );
187+
188+ if ($ this ->layout !== null && $ useLayout ) {
189+ $ data ['content ' ] = $ output ;
190+ $ output = $ this ->fetchTemplate ($ this ->layout , $ data );
191+ }
192+
193+ return $ output ;
194+ }
195+
196+ /**
197+ * Renders a template and returns the result as a string
198+ *
199+ * cannot contain template as a key
200+ *
201+ * throws RuntimeException if $templatePath . $template does not exist
202+ *
203+ * @param $template
204+ * @param array $data
205+ *
206+ * @return mixed
207+ *
208+ * @throws \InvalidArgumentException
209+ * @throws \RuntimeException
210+ */
211+ public function fetchTemplate ($ template , array $ data = []) {
185212 if (isset ($ data ['template ' ])) {
186213 throw new \InvalidArgumentException ("Duplicate template key found " );
187214 }
@@ -190,27 +217,13 @@ public function fetch($template, array $data = [], $useLayout = false) {
190217 throw new \RuntimeException ("View cannot render ` $ template` because the template does not exist " );
191218 }
192219
193-
194- /*
195- foreach ($data as $k=>$val) {
196- if (in_array($k, array_keys($this->attributes))) {
197- throw new \InvalidArgumentException("Duplicate key found in data and renderer attributes. " . $k);
198- }
199- }
200- */
201220 $ data = array_merge ($ this ->attributes , $ data );
202221
203222 try {
204223 ob_start ();
205224 $ this ->protectedIncludeScope ($ this ->templatePath . $ template , $ data );
206225 $ output = ob_get_clean ();
207226
208- if ($ this ->layout !== null && $ useLayout ) {
209- ob_start ();
210- $ data ['content ' ] = $ output ;
211- $ this ->protectedIncludeScope ($ this ->layout , $ data );
212- $ output = ob_get_clean ();
213- }
214227 } catch (\Throwable $ e ) { // PHP 7+
215228 ob_end_clean ();
216229 throw $ e ;
0 commit comments