Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a master template in CodeIgniter

I am stuck with a very basic problem. Ook the problem is this that I want a master template in which I can call the header, body and footer. I am unable to send title and css in header and also how can I send multiple css files. I am doing something like this:

This is the code in controller

$data['title'] = 'Login To WePOS';
$data['css']   = base_url().'style/login-box.css';

$this->load->view('templates/default',$data);   

This is the code in header

<head>
<title><?php echo $title ?> - CodeIgniter 2 Tutorial</title>
    <link href=" <?php echo $css;?>" rel="stylesheet" type="text/css" />
</head>

This is the code in template name as default

<html>
<?php 
$this->load->view('templates/header', $data);
?>
<body>

<?php 
     $this->load->view('login/index', $data);
?>
</body>
<?php 
     $this->load->view('templates/footer', $data);
 ?>
</html>
like image 363
theGame Avatar asked Dec 17 '22 03:12

theGame


2 Answers

hi there is different method to use template in codeigniter .

1- you can use this procedure

In controller

 $data['main_content'] = 'login_view';  
 $data['title']        = 'Login To WePOS';  
 $data['css']          = 'login-box.css';  
 $this->load->view('templates/default',$data);

In template.php View

$this->load->view('header_view');  
 $this->load->view($main_content);   
 $this->load->view('footer_view');  

in your main content variable you can pass the view file

if you want to add multiple css or multiple js files you can use MY_MARK idea as

$data['cssFiles'] = array(
    'login-box.css',
    'other.css'
);  

and in your header file

 if(is_array($cssFiles)){
    foreach($cssFiles as $cssFile) {
        <link href="<?php echo base_url() . 'style/' . $css; ?>" rel="stylesheet" type="text/css" />
    }
}

Hope it helps.

like image 174
chhameed Avatar answered Jan 01 '23 18:01

chhameed


You don't have to pass $data again in your default template.

<html>
    <?php $this->load->view('templates/header'); ?>
    <body>
        <?php $this->load->view('login/index'); ?>
    </body>
    <?php $this->load->view('templates/footer'); ?>
</html>

This should allow you to pick up the $title and $css variables in your header as you have got currently.

With regards to sending multiple css files, create an array of files, like:

$data['cssFiles'] = array(
    'login-box.css',
    'other.css'
);

And modify the code in your header to be:

foreach($cssFiles as $cssFile) {
    <link href="<?php echo base_url() . 'style/' . $css; ?>" rel="stylesheet" type="text/css" />
}

Hope that helps...

like image 35
MY_Mark Avatar answered Jan 01 '23 17:01

MY_Mark