Now we'll try going for a little more complex example, using the capabilities of the controller to fill in the view.
Here is our view:
/application/views/hello_world.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello World</title>
</head>
<body>
<h1><?php echo $greetings;?></h1>
</body>
</html>
Now we have a placeholder for our greetings to be displayed.
Here is how we change the controller in order for this to work:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Hello_world extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function greetings(){
$data = array('greetings'=>'Hello World');
$this->load->view('hello_world',$data);
}
}
The $data
array is prepared with the information to be injected into the view, using the same label (greetings
) that has been recalled inside the view.
The final result is the same as with the first example, but we are now using more of the potentiality of the framework!