Menus

Tuesday, 13 August 2019

Codelgniter - Views


Views

A view is simply a web page, or a page fragment, like 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 never called directly, they must be loaded by a controller. Remember that in an MVC framework, the Controller acts as the traffic cop, so it is responsible for fetching a particular view. If you have not read the Controllers page you should do so before continuing.

Creating a View

Using your text editor, create a file called blogview.php, and put 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 particular view file you will use the following method:
$this->load->view('name');

Adding Dynamic Data to the View

Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading method. Here is an example using an array:
$data = array(
        'title' => 'My Title',
        'heading' => 'My Heading',
        'message' => 'My Message'
);

$this->load->view('blogview', $data);
And here’s an example using an object:
$data = new Someclass();
$this->load->view('blogview', $data);




More Details




No comments:

Post a Comment