我想将一些html代码分配给变量,并且该变量位于控制器中。
<header id="header" class="header">
<div class="container">
<!-- LOGO -->
<div class="logo"><a href="<?php echo base_url()?>admin/Adminhome"><img src="<?php echo base_url()?>public/images/logo.png" alt=""></a></div>
<!-- END / LOGO -->
<!-- NAVIGATION -->
<nav class="navigation">
<div class="open-menu">
<span class="item item-1"></span>
<span class="item item-2"></span>
<span class="item item-3"></span>
</div>
<!-- MENU -->
<ul class="menu">
<?php if($isAdmin ){ ?>
<?php
if($title=='Dashboard'){
?>
<li class="current-menu-item"><a href="<?php echo base_url()?>admin/Adminhome">Admin Dashboard</a></li>
<?php
}else{
?>
<li><a href="<?php echo base_url()?>admin/Adminhome">Admin Dashboard</a></li>
<?php
}
?>
<?php } else { ?>
<?php
if($title=='Dashboard'){
?>
<li class="current-menu-item"><a href="<?php echo base_url()?>admin/Userhomepage">User Dashboard</a></li>
<?php
}else{
?>
<li><a href="<?php echo base_url()?>admin/Userhomepage">User Dashboard</a></li>
<?php
}
?>
<?php } ?>
<?php
if($title=='Courses' || $title=='Add/Edit Categories' || $title=='Upload Manager'){
?>
<li class="current-menu-item">
<a href="<?php echo base_url()?>admin/Coursedetails/courses">Courses</a>
<ul class="sub-menu">
<li><a href="<?php echo base_url()?>admin/Categories">Categories</a></li>
<li><a href="<?php echo base_url()?>admin/coursedetails/courses">Courses</a></li>
<li><a href="<?php echo base_url()?>admin/uploadmanager">Upload Manager</a></li>
</ul>
</li>
<?php
}else{
?>
<li>
<a href="<?php echo base_url()?>admin/Coursedetails/courses">Courses</a>
<ul class="sub-menu">
<li><a href="<?php echo base_url()?>admin/Categories">Categories</a></li>
<li><a href="<?php echo base_url()?>admin/coursedetails/courses">Courses</a></li>
<li><a href="<?php echo base_url()?>admin/uploadmanager">Upload Manager</a></li>
</ul>
</li>
<?php
}
?>
<?php
if($title=='Users' || $title=='User Roles' || $title=='Subscription'){
?>
<li class="current-menu-item">
<a href="<?php echo base_url()?>admin/Users">Users</a>
<ul class="sub-menu">
<li><a href="<?php echo base_url()?>admin/Roles">Roles</a></li>
<li><a href="<?php echo base_url()?>admin/Subscription">Subscription</a></li>
</ul>
</li>
<?php
}else{
?>
<li>
<a href="<?php echo base_url()?>admin/Users">Users</a>
<ul class="sub-menu">
<li><a href="<?php echo base_url()?>admin/Roles">Roles</a></li>
<li><a href="<?php echo base_url()?>admin/Subscription">Subscription</a></li>
</ul>
</li>
<?php
}
?>
<?php
if($title=='Sales' || $title=='Payout Details'){
?>
<li class="current-menu-item">
<a href="<?php echo base_url()?>admin/Transaction">Sales</a>
<ul class="sub-menu">
<li><a href="<?php echo base_url()?>admin/Transaction">Transactions</a></li>
<li><a href="<?php echo base_url()?>admin/Transaction/payout">Payouts</a></li>
</ul>
</li>
<?php
}else{
?>
<li>
<a href="<?php echo base_url()?>admin/Transaction">Sales</a>
<ul class="sub-menu">
<li><a href="<?php echo base_url()?>admin/Transaction">Transactions</a></li>
<li><a href="<?php echo base_url()?>admin/Transaction/payout">Payouts</a></li>
</ul>
</li>
<?php
}
?>
</ul>
<!-- END / MENU -->
<!-- LIST ACCOUNT INFO -->
<ul class="list-account-info">
<!-- MESSAGE INFO -->
<!-- END / MESSAGE INFO -->
<!-- NOTIFICATION -->
<!-- END / NOTIFICATION -->
<li class="list-item account">
<?php
$userId=$this->session->userdata('cp_adminid');
$profilePic="";
$userQ=$this->Adminmodel->getuser();
foreach ($userQ->result() as $rowuser){
$profilePic=$rowuser->profilePic;
}
?>
<div class="account-info item-click">
<?php if($profilePic!=''){ ?>
<img alt="" src="<?php echo base_url();?>private/<?php echo $userId;?>/<?php echo $profilePic;?>">
<?php }else{?>
<img src="<?php echo base_url()?>public/images/default-profile_pic.png" alt="">
<?php }?>
</div>
<div class="toggle-account toggle-list">
<ul class="list-account">
<li><a href="<?php echo base_url()?>admin/settings/"><i class="icon md-config"></i>Setting</a></li>
<li><a href="<?php echo base_url()?>admin/Adminlogout"><i class="icon md-arrow-right"></i>Sign Out</a></li>
</ul>
</div>
</li>
</ul>
<!-- END / LIST ACCOUNT INFO -->
</nav>
<!-- END / NAVIGATION -->
</div>
</header>
此标题出现在应用程序的所有页面中。所以我希望它分配一个名为headerContent
的变量,它位于Admin_controller中。该渲染函数从任何另一个扩展Admin_controller的控制器调用,然后将headeview加载到那些页面。
class Admin_controller extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->model("Adminmodel","",true);
$headerview = 'headerview';
$this->render($headerview); # calling render() function in same class
}
protected $headerview = 'headerview';
protected function render($headerContent) {
$view_data = array( 'headerContent' => $headerContent);
$this->load->view($this->headerview);
}
}
下面的另一个控制器扩展了Admin_controller,称为此render
函数。
<?php
require APPPATH . '/controllers/admin/Admin_controller.php';
class Adminhome extends Admin_Controller{
public function __construct(){
parent::__construct();
$this->load->model("Adminmodel","",true);
public function index(){
$data['content']=$this->load->view("admin/adminhomeview",'',true);
$data['title']='Dashboard';
$adminId=$this->session->userdata('cp_adminid');
$permissions=$this->Adminmodel->getpermissions($adminId);
$row = $permissions->row();
$data['isAdmin'] = 0;
if($row->view == 1 or $row->add == 1 or $row->edit == 1 or $row->deleteRole == 1 ){
$data['isAdmin'] = 1;
}
$this->render($headerContent);
// $this->load->view("admin/headerview",$data);
}
}
?>
但它在Adminhome_controller.php中显示错误,如Message:Undefined variable:headerContent。
答案 0 :(得分:1)
您需要创建一个包含
的模板视图文件模板视图:
<html>
<?php $data = array('permission' => $permission); ?>
<?php $this->load->view('templateincludes/header',$data); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view('templateincludes/footer'); ?>
</html>
CONTROLER:
$data = array('permission' => '1', 'main_content' => 'folder_to_your_view/view_file_you_want_to_load');
$this->load->view('template', $data);
标题视图:
<?= $permission ?>
答案 1 :(得分:0)
但它显示错误,如消息:未定义变量:headerContent 在Adminhome_controller.php中
扩展类中没有名称为$headerContent
的变量。好像你想要访问父/基类的变量。
要解决此问题,您可以在Parent Controller (Admin_controller.php)中声明受保护的字段,并在子控制器内访问它:
class Admin_controller extends CI_Controller{
protected $headerContent; // declare protected field
protected $headerview = 'headerview';
function __construct()
{
parent::__construct();
$this->load->model("Adminmodel","",true);
$headerview = 'headerview';
$this->render($headerview); # calling render() function in same class
}
protected function render($headerContent) {
$view_data = array( 'headerContent' => $headerContent);
$this->headerContent = $headerContent; // assign value into $headerContent field
$this->load->view($this->headerview);
}
}
您可以使用以下方式直接在子类(Adminhome_controller.php)中访问它:
$this->render($this->headerContent);