简体   繁体   English

如何从codeigniter的主模板加载选定的功能

[英]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>

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 :您必须更改主视图模板才能实现此目标,一种方法是设置与登录方法匹配的条件逻辑,例如,如果page_title值为User Login ,则不要加载正文内容,而是为其提供登录视图模板,否则加载主体内容等:

<!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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM