0
votes

I'm new to CodeIgniter and I'm trying to integrate templates with my views. For each view, I wanted to have a header, main content, and footer for the template. However, the headers for each of my pages are different since their css are also different. The footer, which I have scripts, also have different codes. Do I need to create separate headers and footers for each of the pages? Here's an example.

Adticket_footer.php

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js">
        $('#myModal').modal() // initialized with defaults
    </script>

    <!-- Menu Toggle Script -->
    <script>
        $("#menu-toggle").click(function (e) {
            e.preventDefault();
            $("#wrapper").toggleClass("toggled");
        });
    </script>
  </body>
</html>

login_footer.php

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

Files

1
You want to create a same footer file having code of multiple footer depends on page? - Akshay
I don't really have an idea what approach to do, since for the headers for example, they are linked to different css files and putting them together will result to conflicting css designs - coderszx
you will be having different css files for different headers right? - Akshay
Yes, so is there a better approach to doing these headers and footers than my approach above, which is having different headers and footers for different pages? - coderszx

1 Answers

0
votes

I would normally have one header and one footer. For additional css for instance you can simply set an additional item for your header to include.

In your controller

$header_data['additional_css'] = array('custom_css_1.css', 'custom_css_2.css');

$this->load->view('header_view', $header_data);

In your view you can add them then:

<?php if(!empty($additional_css)) { ?>
   <?php foreach ($additional_css as $file_name) { ?>
       <link rel="stylesheet" href="<?php echo base_url('assets/css/'.$file_name); ?>">
   <?php } ?>
<?php } ?>

I do an array for css files rather than passing a single variable as often you have to add more than one. If you do not set any in your controller then nothing will be added. (I do assume here it is an array but technically you should also check it is an array as well.)

You can do the same in your footer, allowing you to add js files or js. I normally would have four additionals, one for css files, one for inline css, one for js files and one for inline js. For inline js or css you can just use a variable with the content you want to display rather than an array.

There are other solutions of course if you use smarty or some other template system (they are quite easy to integrate into CI, and libraries are available) but I find using these additional arrays is simple, quick and very flexible.