phphtmlcodeignitercodeigniter-3loadview

How to load selected function from master template in codeigniter


Is it possible to load a selected method only in my Master Template? I want to load only the header and some scripts from my master and avoid the remainder of the template.

This is my controller for my login which I want to load only the header and footer scripts, and I want to avoid some functions from master template.

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Auth extends MY_Controller {


    public function index(){
        $this->data['page_title'] = "User Login";
        $this->load->view('templates/master', $this->data);

    }

    public function login(){
        $email = $_POST['email'];
        $password = $_POST['password'];

        $data = $this->User_model->login ($email, $password);

        if($data){  
        $this->session->set_userdata('users', $data);
        redirect('users');


        }
        else{
            $this->session->set_flashdata
            ('loginfail','<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
            <strong>Danger !</strong> Invalid Email or Password .</div>');
           return redirect("auth");


        } 
    }
}

This is my master template

<!DOCTYPE html>
<html lang="en">
    <head>
        <?php $this->load->view('templates/sections/head'); ?>
    </head>

    <body id="page-top">

        <!-- Page Wrapper -->
        <div id="wrapper">

            <?php $this->load->view('templates/sections/nav'); ?>

            <!-- Content Wrapper -->
            <div id="content-wrapper" class="d-flex flex-column">

                <!-- Main Content -->
                <div id="content"> 

                    <?php $this->load->view('templates/sections/top_bar'); ?>

                    <!-- Begin Page Content -->
                    <div class="container-fluid"> 

                        <!-- Page Heading -->
                        <h1 class="h3 mb-4 text-gray-800"><?php echo $page_title; ?></h1>

                        <?php $this->load->view(CONTROLLER.'/'.METHOD); ?>

                    </div> <!-- /.container-fluid -->

                </div> <!-- End of Main Content -->

                <!-- Footer -->
                <footer class="sticky-footer bg-white">
                    <?php $this->load->view('templates/sections/copyright'); ?>
                </footer>
                <!-- End of Footer -->

            </div> <!-- End of Content Wrapper -->

        </div> <!-- End of Page Wrapper -->

        <?php $this->load->view('templates/sections/additional_footer_scripts.php'); ?>

        <?php $this->load->view('templates/sections/foot.php'); ?>

    </body>
</html>

This is my log-in view

<body class="bg-gradient-primary">
<div class="container">

    <!-- Outer Row -->
    <div class="row justify-content-center">

      <div class="col-xl-10 col-lg-12 col-md-9">

        <div class="card o-hidden border-0 shadow-lg my-5">
          <div class="card-body p-0">
            <!-- Nested Row within Card Body -->
            <div class="row">
              <div class="col-lg-6 d-none d-lg-block bg-login-image"></div>
              <div class="col-lg-6">
                <div class="p-5">
                  <div class="text-center">
                  <h1 class="h4 text-gray-900 mb-4">Welcome Back!</h1>
                  </div>







<form method="POST" action="<?php echo base_url(); ?>index.php/Auth/login"> <div class="alert alert-error">
<?php 
if($error=$this->session->flashdata('loginfail'))
{
echo $error;
}
?>
    <div class="form-group">
            <input placeholder="Email" class="form-control form-control-user" type="email" name="email" required>        
    </div>
    <div class="form-group">
            <input  placeholder="Password" type="password" class="form-control form-control-user" name="password" required>
    </div>

    <div class="form-group">
            <div class="custom-control custom-checkbox small">
            <input type="checkbox" class="custom-control-input" id="customCheck">
            <label class="custom-control-label" for="customCheck">Remember Me</label>
    </div>

            <button  class="btn btn-primary btn-user btn-block" type="submit" > Login</button>
    <hr>
</form>

Solution

  • You have to alter the master view templates to achieve this goal, one way is to set a conditional logic that match the login method, for example if the page_title value is User Login, then don't load the body content but instead supply it with the login view template, otherwise load the master body content, etc :

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <?php $this->load->view('templates/sections/head'); ?>
        </head>
    
        <?php
        if ($page_title == 'User Login') {
            $this->load->view('templates/login'); // example login view
        } else {
        ?>
    
        <body id="page-top">
    
            <!-- Page Wrapper -->
            <div id="wrapper">
    
                <?php $this->load->view('templates/sections/nav'); ?>
    
                <!-- Content Wrapper -->
                <div id="content-wrapper" class="d-flex flex-column">
    
                    <!-- Main Content -->
                    <div id="content"> 
    
                        <?php $this->load->view('templates/sections/top_bar'); ?>
    
                        <!-- Begin Page Content -->
                        <div class="container-fluid"> 
    
                            <!-- Page Heading -->
                            <h1 class="h3 mb-4 text-gray-800"><?php echo $page_title; ?></h1>
    
                            <?php $this->load->view(CONTROLLER.'/'.METHOD); ?>
    
                        </div> <!-- /.container-fluid -->
    
                    </div> <!-- End of Main Content -->
    
                    <!-- Footer -->
                    <footer class="sticky-footer bg-white">
                        <?php $this->load->view('templates/sections/copyright'); ?>
                    </footer>
                    <!-- End of Footer -->
    
                </div> <!-- End of Content Wrapper -->
    
            </div> <!-- End of Page Wrapper -->
    
            <?php $this->load->view('templates/sections/additional_footer_scripts.php'); ?>
    
            <?php $this->load->view('templates/sections/foot.php'); ?>
    
        </body>
    
        <?php
        }
        ?>
    
    </html>