5.3 嵌套视图和Partials

在视图中嵌套其他视图是一种非常方便的机制。在本章后面你将会看到,当使用布局视图时,这是一种常见的做法。在一个视图中嵌入另一个视图有两种方法可以实现:

5.3.1 方法一:使用Solar_View::template()方法

<h1>Example Page</h1>
<?php 
// view script: index.php
// include another view
include $this->template('_list'); // adding the .php extension is optional
?>

Solar_View::template()方法的参数是一个视图脚本,返回值是视图脚本的完整路径。

这里,嵌套视图(如:代码中的_list视图)可以使用主视图所有的变量/属性。

5.3.2 方法二:使用Solar_View::partial()方法

<h1>Example Page</h1>
<?php 
// view script: index.php
// add a another view as a partial
echo $this->partial('_list', $this->list); // adding the .php extension is optional
?>

此方法和Solar_View::template()方法类似,但是,Partial有自己的作用域。它不能使用主视图的变量/属性。给Partial传递任何数据都必须通过方法的第二个参数(可选)。详细信息请查看Solar_View的API文档。