如何在代码点火器中从主模板加载所选函数



是否可以仅在我的主模板中加载选定的方法?我只想从我的主节点加载标头和一些脚本,并避免模板的其余部分。

这是我登录的控制器,我只想加载页眉和页脚脚本,并且我想避免主模板中的某些功能。

<?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");

} 
}
}

这是我的主模板

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

这是我的登录视图

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

您必须更改主视图模板才能实现这一目标,一种方法是设置与登录方法匹配的条件逻辑,例如,如果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>

相关内容

  • 没有找到相关文章

最新更新