Search code examples
codeignitermaster-pages

dynamic master template in codeigniter


My problem is this:

I have a masterpage:

<head>
    <meta charset="utf-8" />
    <title><?php echo isset($title)? $title: NULL; ?></title>
    <?php $this->load->view('layout/header'); ?>
</head>
<body>           
     <!-- BEGIN PAGE BASE CONTENT -->
         <?php $this->load->view($content);?>
     <!-- END PAGE BASE CONTENT -->       
</body>

and my dashboard controller:

class Dashboard extends CI_Controller {

    public function index()
    {
      if($this->session->userdata('login') == true){
        $data['title'] = 'Dashboard';
        $data['content'] = 'pages/dashboard';
        $this->load->view('layout/master', $data);
    }
    else{
        redirect('auth');
    }
    }

and my add controller:

if($this->session->userdata('login') == true){
    $data['title'] = 'افزودن مشتری';
    $data['content'] = 'pages/add_customer';
    $this->load->view('layout/master', $data);
}
else{
    redirect('auth');
}

My problem is that at first when i call dashboard, everything is OK. but when i call add, everything mess up like there is no CSS attached or something. Should i do something before set value to $content? I cant understand what the problem is.


Solution

  • When in a function you can't do anything with it. But can't do anything outside. Like your else statement.

    public function add(){
        $data['title'] = 'افزودن مشتری';
        $data['content'] = 'pages/add_customer';
        $this->load->view('layout/master', $data);
    }
    else{
        redirect('auth');
    }
    

    This is completely wrong practices. Just use

    public function add(){
        $data['title'] = 'افزودن مشتری';
        $data['content'] = 'pages/add_customer';
        $this->load->view('layout/master', $data);
    }
    

    Load Codeigniter for the Arabic Letters

    Codeigniter by default is set to use UTF-8 for much of its internal functionality, so just make sure the charset is set to UTF-8 in your application/config/config.php file.

    $config['charset'] = "UTF-8";
    

    And set the header too

    header('Content-Type: text/html; charset=utf-8');