bmrinal
bmrinal

Reputation: 365

Placing of css in codeigniter views

I am new to codeigniter. I made a simple controller to load the three views: header, body and footer. My header contains links to several javascripts and CSS which have a relative paths. I kept all the css and js relative to file in views folder.

The problem is that the controller loads everything properly but images, js and css are not getting loaded.

This is the hierarchy of my view:

CSS(sub directory in views) JS (sub directory in views)

header (header view file) body (body view file) footer (footer view file)

ny idea?

Upvotes: 4

Views: 23252

Answers (4)

Dale Hurley
Dale Hurley

Reputation: 151

You need to put the files in the base directory e.g. the one containing the application and system folders.

Try using a folder called assests.

Also make sure your are using a leading slash so e.g.

href="/assets/css/style.css"

Note this will load from base URI

e.g. example.local/assets/css/style.css

Upvotes: 4

Jonathan Chow
Jonathan Chow

Reputation: 703

It is not a good idea to put everything in your view file, as those folders are supposed to clean things up, putting them in view makes them messy again.

Assume this is your directories:

Localhost

  • Application
    • Controllers
    • Views
    • Etc..
  • System
  • Assets
    • Javascripts
    • CSS

To include any of javascripts, the path would be:

localhost/Assets/Javascripts/your_script.js

and obviously CSS would be

localhost/Assets/CSS/your_stylesheet.css

In order to make sure your implementation work everywhere,

<script src="<?=base_url('path/to/your/js/foo.js')>" type="text/javascript"></script>

would be the safest way

Upvotes: 6

Brad
Brad

Reputation: 1691

The best way to link to css files is like this

echo link_tag('css/reset.css');

Note the link tag does not use the / while the hs file below does

Your css and js needs to be in the root directory

JS files

<script defer src="/media/javascripts/scrips.js"></script>

Also in the root. Of coruse your directory will be different

You cannot link to css files that are in the same view directory

http://codeigniter.com/user_guide/helpers/html_helper.html

Upvotes: 0

He Hui
He Hui

Reputation: 2236

I would suggest you to not keep your js/images/css files in the view folder.

Please refer to this past question. Where do I put image files, css, js, etc. in Codeigniter?

As to answer your question, it is like Dale said.

First you will need to check your .htaccess, and note where is your base directory.

You'll then have to provide a path link that is appropriate to your js/css folder. If you kept them in your views folder, it seems like you'll have to do this url

http://localhost/codeigniter/index.php/application/views/css/text.css

If I am not wrong, you are currently at

"http://localhost/codeigniter/index.php/admin"

trying to load the css files. If you use href="something", it will merely add on to your current URL. Which will provide "http://localhost/codeigniter/index.php/admin/something" which IMO is not what you're looking for.

Upvotes: 2

Related Questions