Codeigniter 2.20 – How to manage assets (js, jpg etc) (using View)


Back to CodeIgniter Tutorials

In this guide, you will learn to use CSS and JS files by calling them from the VIEW (not the controller). In the coming lessons, I will also write on importing assets using Controller but here, we are using views.

  • First, install Codeigniter using the method as described here.
  • Then make your own homepage using the method explained here.

Now that we are set up, lets get started!

  1. Go to your Codeigniter folder and add a folder called assets such that it should look like this yourwebsite/assets/ For reference, look at the following

    codeigniter assets folder
    codeigniter assets folder
  2. Inside /assets/ create three folders which should look like this
    1. CSS folder named css
    2. Javascript folder named javascript 
  3. In the css folder, create style.css
  4. In the javascript folder, create scripts.js
  5. Go to your homepage and paste the following in the <head> tag.
    <script src="http://localhost/yourwebsite/assets/javascript/my-script.js">
    css" href="http://localhost/yourwebsite/assets/css/style.css">
  6. That is all. You can similarly add jquery and images in the same fashion. In order to confirm that the CSS and javascript is loaded, you can check source of the loaded page and click on the .css and .js files

Please note that this guide is applicable on using XAMPP. For web server, you may have different folder paths for example:

<link rel="stylesheet" href="<?php echo base_url("assets/cover.css"); ?>">

I hope you find this tutorial informational. Do not forget to comment and share 🙂

Back to CodeIgniter Tutorials

,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: