Views
Directory Structures & Files
Views are written in PHP and HTML and have a .php
file extension.
View scripts are placed in /com_{componentname}/views/
, where they are further categorized by the /{viewname}/tmpl
.
Within these subdirectories, you will then find and create view scripts that
correspond to each controller action exposed; in the default case, we have the view script default.php
.
/hubzero
/components
/com_{componentname}
/views
/{viewname}
/tmpl
default.php
Overriding module and component presentation in templates is further explained in the Templates: Overrides section.
Creating A View
The task of the view is very simple: It retrieves the data to be displayed and pushes it into the template.
// Instantiate a new view $view = new \Hubzero\Component\View(array( 'name' => $this->_controller, 'layout' => 'foo' )); // Assign data to the view $view->greetings = 'Hello'; // Echo out the results $view->display();
In the above example, the view constructor is passed an array of options. The two most important options are listed: name
, which is the folder to look for the view file in and will typically correspond to the current controller's name, and layout
, which is the specific view file to load. If no layout
is specified, the layout is typically auto-assigned to the current task name. So, if the controller in the example code is one
, the directory structure would look as follow:
/com_example /views /one /tmpl /foo.php
Method Chaining
All Hubzero view objects support method chaining for brevity and ease of use.
// Instantiate a new view $view = new \Hubzero\Component\View(array( 'name' => $this->_controller, 'layout' => 'foo' )); $view->set('greetings', 'Hello') ->setLayout('bar') ->display();