CodeIgniter Responses

Creating a view

Create a new file in views folder, write HTML there.

Displaying a view

use view() function in controller to load view file

echo view('view_name');

Storing views in subdirectory 

echo view('directory_name/view_name')

Adding dynamic data to view

$data=[
"id"=>1,
"name"=>"Rama"
];
echo view('users', $data);

Access dynamic data at view

<?= $id ?>
<?= $name ?>

Alternate PHP Syntax at CodeIgniter

Alternative of echo

<?= $name ?>

Alternative of if else

if( $amount > 2000):
         $discount= $amount*5/100;
endif

Alternative of foreach

<ul>
foreach($items as $item):
    <li><?= $item ?></li>
endforeach
</ul>

View layout

<html>
<head>
    <title>My Layout</title>
</head>
<body>
    <?= $this->renderSection('content') ?>
</body>
</html>

Using layout in view

<?= $this->extends('layout_name') ?>
<?= $this->section('content') ?>
....................... write data here
<?= $this->endSection() ?>

Include partial view

<?= $this->include('navbar') ?>
© 2016 - 2022, All Rights are Reserved.