Codeigniter View
A read is just an online page, or a page fragment, sort of a header, footer, sidebar, etc.
In fact, views can flexibly be embedded within other views (within other views, etc., etc.) if you need this type of hierarchy.
Views are ne'er known as directly, they have to be loaded by a controller.
You should be remembered that in AN MVC framework, the Controller acts as the traffic cop, so it is responsible for fetching a particular view.
If you have got not scan the Controllers page you must do therefore before continued.
Using the example controller you created on the controller page, let’s add a view to it.
Creating a View:
Using your text editor, produce a file known as blogview.php, and place this in it:
<html>
<head>
<title>My Blog</title>
</head>
<body>
<h1>Welcome to my Blog!</h1>
</body>
</html>
Then save the file in your application/views/ directory.
Loading a View:
To load a specific read file you may use the subsequent method:
Where name is that the name of your read file.
Note - The .php file extension doesn't ought to be given unless you utilize one thing apart from .php.
Now, open the controller file you created earlier known as journal.php, and replace the echo statement with the read loading method:
<?php
class Blog extends CI_Controller {
public function index()
{
$this->load->view('blogview');
}
}
If you visit your website using the URL you probably did earlier you ought to see your new read.
The URL was similar to this:
Codeigniter Multiple Views
CodeIgniter can showing intelligence handle multiple calls to $this->load->view() from at intervals a controller.
If quite one decision happens they're going to be appended along.
For example, you will like to possess a header read, a menu read, a content read, and a footer read.
That might look something like this:
<?php
class Page extends CI_Controller {
public function index()
{
$data['page_title'] = 'Your title';
$this->load->view('header');
$this->load->view('menu');
$this->load->view('content', $data);
$this->load->view('footer');
}
}
Storing View within Sub-Directories
Your read files may be kept among sub-directories if you like that variety of organizations.
When doing this you may have to be compelled to embrace the directory name loading the read. Example:
Adding dynamic data to view
Data is passed from the controller to the read by a method of an associate array or associate object within the second parameter of the read loading technique.
Here is an example using an array:
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('blogview', $data);
Now open your read file and alter the text to variables that correspond to the array keys in your data:
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<h1><?php echo $heading;?></h1>
</body>
</html>