Views are how you display your data. Separating these from application code keeps your application logic neat and concise. It also prevents markup changes from triggering large code changes.
$view = ee('View')->make('addon_name:member/profile'); $output = $view->render(array( 'member' => $current_member ));
All views are represented as separate objects. To create a new view instance, simply call
make() on the core view service and pass in a the path to a view file, prefixed with the folder name of your add-on:
$view = ee('View')->make('addon_name:member/profile');
The view file should be a plain
.php file in the
views folder. In this case it would be:
To render a view, call the
render() method. You can optionally pass an associative array to use as the view data:
$view->render(array('username' => 'Mike'));
The keys of this array will be accessible as local variables inside the view:
Reusing a view sometimes requires small reductions in markup. For example, a profile image may require some wrapping markup for most of the site, but not universally. To avoid passing around boolean values, views can have have disabled features:
In the view, these can be checked using the
$this->enabled() helper methods:
if ($this->enabled('figure')): <figure> =$username </figure> endif;
Note: By default all features are enabled.
Views can be rendered directly inside of another view. This is done using the
$this->embed() helper method:
All of the current view variables are automatically made available to the subview. You can optionally pass additional ones in the second parameter:
<p>$this->embed('addon_name:sub/view', array('username' => $member->username)) </p>
Notice that you do not need to echo the output of this method, it is added to the right place automatically.
You can also disable features in a view, using the third parameter:
<p>$this->embed('addon_name:sub/view', array(), 'figure') </p>
A view can extend another view, by calling the
$this->extend() helper method:
The rendered child view will be available to the parent as
The API for this this method is otherwise identical to
embed(). The second and third parameter are used for additional variables and disabled features, respectively.
Passing large chunks of data from a child view to a parent can be tedious. To solve this problem, the child view can create blocks of content that are passed to the parent view automatically:
$this->extend('html_wrapper') $this->startBlock('js') <script> ... </script> $this->endBlock()
This data is passed to the parent view as an associative array called