@@ -23,21 +23,16 @@ class ViewRenderer
2323 */
2424 protected $ viewsPath ;
2525
26- /**
27- * 默认布局文件
28- * @var string
29- */
26+ /** @var null|string 默认布局文件 */
3027 protected $ layout ;
3128
32- /**
33- * @var array
34- */
29+ /** @var array Attributes for the view */
3530 protected $ attributes ;
3631
37- /** @var string */
32+ /** @var string Default view suffix. */
3833 protected $ suffix = 'php ' ;
3934
40- /** @var array allowed suffix list */
35+ /** @var array Allowed suffix list */
4136 protected $ suffixes = ['php ' ,'tpl ' ,'html ' ];
4237
4338 /**
@@ -118,6 +113,81 @@ public function renderContent($content, array $data = [], $layout = null)
118113 return $ content ;
119114 }
120115
116+ /**
117+ * @param $view
118+ * @param array $data
119+ * @param bool $outputIt
120+ * @return string|null
121+ * @throws \Throwable
122+ */
123+ public function include ($ view , array $ data = [], $ outputIt = true )
124+ {
125+ if ($ outputIt ) {
126+ echo $ this ->fetch ($ view , $ data );
127+ return null ;
128+ }
129+
130+ return $ this ->fetch ($ view , $ data );
131+ }
132+
133+ /**
134+ * Renders a view and returns the result as a string
135+ * throws RuntimeException if $viewsPath . $view does not exist
136+ * @param string $view
137+ * @param array $data
138+ * @return mixed
139+ * @throws \Throwable
140+ */
141+ public function fetch ($ view , array $ data = [])
142+ {
143+ $ file = $ this ->getViewFile ($ view );
144+
145+ if (!is_file ($ file )) {
146+ throw new \RuntimeException ("cannot render ' $ view' because the view file does not exist. File: $ file " );
147+ }
148+
149+ /*
150+ foreach ($data as $k=>$val) {
151+ if (in_array($k, array_keys($this->attributes))) {
152+ throw new \InvalidArgumentException("Duplicate key found in data and renderer attributes. " . $k);
153+ }
154+ }
155+ */
156+ $ data = array_merge ($ this ->attributes , $ data );
157+
158+ try {
159+ ob_start ();
160+ $ this ->protectedIncludeScope ($ file , $ data );
161+ $ output = ob_get_clean ();
162+ } catch (\Throwable $ e ) { // PHP 7+
163+ ob_end_clean ();
164+ throw $ e ;
165+ }
166+
167+ return $ output ;
168+ }
169+
170+ /**
171+ * @param $view
172+ * @return string
173+ */
174+ public function getViewFile ($ view )
175+ {
176+ $ view = $ this ->getRealView ($ view );
177+
178+ return File::isAbsPath ($ view ) ? $ view : $ this ->viewsPath . $ view ;
179+ }
180+
181+ /**
182+ * @param string $file
183+ * @param array $data
184+ */
185+ protected function protectedIncludeScope ($ file , array $ data )
186+ {
187+ extract ($ data , EXTR_OVERWRITE );
188+ include $ file ;
189+ }
190+
121191 /**
122192 * Get the attributes for the renderer
123193 * @return array
@@ -154,7 +224,7 @@ public function addAttribute($key, $value)
154224 public function getAttribute ($ key )
155225 {
156226 if (!isset ($ this ->attributes [$ key ])) {
157- return false ;
227+ return null ;
158228 }
159229
160230 return $ this ->attributes [$ key ];
@@ -198,69 +268,6 @@ public function setLayout($layout)
198268 $ this ->layout = rtrim ($ layout , '/ \\' );
199269 }
200270
201- /**
202- * Renders a view and returns the result as a string
203- * cannot contain view as a key
204- * throws RuntimeException if $viewsPath . $view does not exist
205- * @param $view
206- * @param array $data
207- * @return mixed
208- * @throws \Throwable
209- */
210- public function fetch ($ view , array $ data = [])
211- {
212- if (isset ($ data ['view ' ])) {
213- throw new \InvalidArgumentException ('Duplicate view key found ' );
214- }
215-
216- $ file = $ this ->getViewFile ($ view );
217-
218- if (!is_file ($ file )) {
219- throw new \RuntimeException ("cannot render ' $ view' because the view file does not exist. File: $ file " );
220- }
221-
222- /*
223- foreach ($data as $k=>$val) {
224- if (in_array($k, array_keys($this->attributes))) {
225- throw new \InvalidArgumentException("Duplicate key found in data and renderer attributes. " . $k);
226- }
227- }
228- */
229- $ data = array_merge ($ this ->attributes , $ data );
230-
231- try {
232- ob_start ();
233- $ this ->protectedIncludeScope ($ file , $ data );
234- $ output = ob_get_clean ();
235- } catch (\Throwable $ e ) { // PHP 7+
236- ob_end_clean ();
237- throw $ e ;
238- }
239-
240- return $ output ;
241- }
242-
243- /**
244- * @param $view
245- * @return string
246- */
247- public function getViewFile ($ view )
248- {
249- $ view = $ this ->getRealView ($ view );
250-
251- return File::isAbsPath ($ view ) ? $ view : $ this ->viewsPath . $ view ;
252- }
253-
254- /**
255- * @param string $file
256- * @param array $data
257- */
258- protected function protectedIncludeScope ($ file , array $ data )
259- {
260- extract ($ data , EXTR_OVERWRITE );
261- include $ file ;
262- }
263-
264271 /**
265272 * @return string
266273 */
0 commit comments