5.3. Nested Views and Partials

It is often convenient to nest views inside other views. This is a common practice when using layouts as you will see later in this chapter. There are two ways to include one view inside of another view.

5.3.1. Option 1: Use the Solar_View::template() method.

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

The Solar_View::template() method returns the full path to the view script that is passed as an argument.

Also, all the properties of the parent view are available to the nested view.

5.3.2. Option 2: Use the Solar_View::partial() method.

<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
?>

This method is similar to the Solar_View::template() method, but here, the partial is in it's own scope. Properties of the parent view are NOT available to this partial view. Any data that you want to pass to the partial, must be passed as the optional second argument. Please see the API docs on Solar_View for more information.

[Note] Note

If you do not need scope separation, using Solar_View::template() is faster.



Local