Views
CodeIgniter provides a very clever way to load views from a controller and pass data into them to be dislayed. Since BackendPro provides you the developer with a pre-built administration area I had to take decisions on how to handle the views for the applcations.
Views should always be seperated into two different folders inside the main views folder, admin and public. The reason for this is not only does it provide a nice way for you to seperate the different files but also means my inbuilt data output process can be used.
Containers
Since BackendPro tries to use modular code as much as possible I wanted the same header/footer code to be used for all pages and just therefore change the content of the page. To do this I implemented a container system. What this is is instead of calling a single view file, you call a master view file which then goes and loads its respective headers and footers etc.
When you extend either of the Admin Controller or the Public Controller a class variable is inherited which you can only access by using $this->_container. This variable is the container view file I was talking about for the respective Controller class.
The master view files and their children view files are located in system/application/views/admin and system/application/views/public respectivly.
Possible Page Content
As said above, I wanted to design a system so content could be passed into a default view file and it would work, for this reason there is three ways to add content to a page. In all three a header variable is set. This sets the text to be displayed in the title bar of the browser.
- Basic plain text
If you just want to display some text and don't require to load a view you can simple pass a string to the master container.function index()
{
$data['header'] = "My Page";
$data['content'] = "Just displays some basic text, nothing more";
$this->load->view($this->_container,$data);
}As you can see in the example above, instead of calling a view file I tell it to load the file defined by $this->_container. The text string is passed as input using the content variable entry.
- View File
For the times when you want to load a more compelx page, passing the data by string can get confusing and messy. For this reason you can specify a page to load in its place (read page as view file).
function index()
{
$data['header'] = "My Page";
$data['page'] = $this->config->item('backendpro_template_admin') . "my_view_file"
$this->load->view($this->_container,$data);
}In the above example you will see I specify a page to load. So in this example it will load the view file system/application/view/admin/my_view_file.php.
- View File in a Module
This case is exactly the same as that above apart from due to the way matchbox is setup you must specify what module you want to load the view file from. For this reason we pass one more value into the master container.
function index()
{
$data['header'] = "My Page";
$data['page'] = $this->config->item('backendpro_template_admin') . "my_view_file"
$data['module'] ='module';
$this->load->view($this->_container,$data);
}In the above example you will see I specify a module to load the view from. So in this example it will load the view file modules/module/view/admin/my_view_file.php.