Fork me on GitHub

Views Within Views

To render a view within a view, use $this->render_view(). For example, to render a view named _tree.php in the views directory of the current controller:

<?php
$this->render_view('_tree');
?>

To render a view from a specific directory, specify the path:

<?php
$this->render_view('documentation_nodes/_tree');
?>

To pass variables into the view, use:

<?php
// index.php
$this->render_view('_tree', array('locals' => array('objects' => $tree_objects)));

// _tree.php
// This will print_r() the $tree_objects that was set in index.php
print_r($objects);
?>

There is a naming convention that views that are used as partials (i.e. small, basic views that are only rendered inside of larger views) should have file names that are prefixed with an underscore (like the _tree.php above). (This convention is borrowed from Rails.)