2

In my CodeIgniter application have a common header and footer view, which I include in different views. My header_view is as simple as:

<!DOCTYPE html>

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
    <title><?php echo $page_title;?></title>
    <link rel="stylesheet" href="<?php echo base_url();?>/css/master.css" type="text/css" media="screen" />
    <script src="<?php echo base_url();?>js/common.js" type="text/javascript"></script> 
</head>
<body>
<!-- end of header -->

Now Lets suppose I have another view called form_view, where I would like to include a CSS file form.css and a javascript file form.js. Since these files are only used in form_view, I don't want to add them in the header_view, because then they will be included in all of the views in my application.

So my question remains, how can I include a specific css or a specific javascript file on a specific view, when using a common header like above.

Thanks.

tereško
  • 58,060
  • 25
  • 98
  • 150
Roman
  • 3,764
  • 21
  • 52
  • 71
  • I'm not sure if using a single view for the whole page is a good idea. I'd split the view in header, body, footer... So in your form controller you could load a different header. – Fabrizio D'Ammassa Jul 15 '11 at 06:43
  • I mentioned above that I have a common header_view and footer_view which I include in all of the views. For example I will include same header_view and footer_view in form_view and other views. – Roman Jul 15 '11 at 06:45
  • Are you using a template library or the CI default one: http://codeigniter.com/user_guide/general/views.html ? I'd like to understand how you have included the header in all of the views... – Fabrizio D'Ammassa Jul 15 '11 at 06:51
  • @Fabrizio D. I am not using any template library, just default CI framework. For example the above code is saved in a view called header_view.php and it is placed in the views folder. Now I have another view called, form_view in the same folder, in that i'd call the header_view by: `load->view('header_view'); ?>`and then rest of the content of the form_view. Similarly i will load the same header_view in another view, login_view. Do you get the idea? – Roman Jul 15 '11 at 07:17
  • Ok, got it. You could load a different header_view just in your form_view or split the header_view in a static part and in dynamic (page based) one. – Fabrizio D'Ammassa Jul 15 '11 at 07:29

1 Answers1

0

You could write something like Zend_Bootstrap and run it before views. The bootstrap file will be responsible for setting css and js files so your header will just grab them from an array.

Bootstrap:

$head_array = array(
   'css' => array(
         '/static/css/style.css', 
         '/static/css/style1.css' 
      ),
   'js' => array(
         '/static/js/script.js'
      )
)

if($view == 'view_form') {
    $head_array['css'][] = '/static/css/form.css';
}

Of course this example is very primitive but it shows the idea.

Norbert Orzechowicz
  • 1,329
  • 9
  • 20