如何从 codeigniter 中的主模板加载选定的函数
How to load selected function from master template in codeigniter
是否可以只在我的主模板中加载选定的方法?我只想加载 header 和我的主人的一些脚本,并避免模板的其余部分。
这是我的登录控制器,我只想加载 header 和页脚脚本,并且我想避免主模板中的某些功能。
<?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">×</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>
这是我的log-in视图
<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>
是否可以只在我的主模板中加载选定的方法?我只想加载 header 和我的主人的一些脚本,并避免模板的其余部分。
这是我的登录控制器,我只想加载 header 和页脚脚本,并且我想避免主模板中的某些功能。
<?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">×</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>
这是我的log-in视图
<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>