CodeIgniter Forums
css file and methods - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: css file and methods (/showthread.php?tid=31113)

Pages: 1 2


css file and methods - El Forum - 06-07-2010

[eluser]mysterious[/eluser]
hi,

I have a controller file and a view file. View file uses a CSS file and javascript files that are placed in main folder. CSS file is placed in main folder with index.php.

The Controller's index function loads CSS file when i dnt call it but when i call controller with method name it does not load.i.e.
http://localhost/project/index.php/controler will load CSS file.
http://localhost/project/index.php/controler/method wont load CSS file.

in my view file i have given path of css file as
<link href="../style.css" rel="stylesheet" type="text/css" />

i also changed it to folder up as
<link href="../../style.css" rel="stylesheet" type="text/css" />, in this case
http://localhost/project/index.php/controler wont load CSS file, and
http://localhost/project/index.php/controler/method will load CSS file.

whats wrong Sad


css file and methods - El Forum - 06-07-2010

[eluser]richzilla[/eluser]
I think you might have misunderstood the way CI works. The index function is simply a default function that is called if no other function has been specified. So if the view your calling through your index function has the CSS reference in it, it wont be called for any other method. If you want something to be called everytime the controller is created, put it in the the constructor of your controller (the one with the same name as your controller or __construct).

A better solution however is simply to link to a view that contains all of your css and js references in your view. This way any modifications you make to it, are instantly repeated on every other page.

Personally, i tend to split my pages up into a header and footer and then any number of views to make up the main body of my page. All of my CSS and JS references are called in the 'header' view, and this is called in each of my controller methods, however, you should use whatever system works best with your particular scenario.


css file and methods - El Forum - 06-07-2010

[eluser]mddd[/eluser]
@ricardino: Your explanation is useful, but I think this is not the problem here.

@mysterious: The problem is that your webbrowser looks at CodeIgniter addresses like any other addresses. The browser doesn't know about controllers, methods, and all the other stuff that is in CodeIgniter. If you use a relative url for a css file, an image or anything like that, your browser will try to find it from where it thinks it is in the directory structure.

What does this mean? It means that you should not use relative urls for things like css files, images and other linked files.
Why? Because you might have a CodeIgniter url like www.example.com/clients/some-client/projects/project123/sort/alphabetical.
If you want to make the browser understand that, you would have to link to ../../../../../../css/mycssfile.css...
And if you added another variable to it, you would also have to add another '..' to the url.

Therefore, the solution is to use absolute urls. Just link to /css/mycssfile and the browser will always be able to find it.
You can use the site_url() function in CI to create the right url, and still be flexible if you want to move the site later on.


css file and methods - El Forum - 06-07-2010

[eluser]richzilla[/eluser]
@mddd - Your right, i just set off with an answer without really reading the question. Spot on answer btw.


css file and methods - El Forum - 06-07-2010

[eluser]mysterious[/eluser]
@ricardino - thnks for the reply, its useful for me anyway Smile


css file and methods - El Forum - 06-07-2010

[eluser]mysterious[/eluser]
@maddd - thnks i got ur point... but how will i give absolute path?

lets say I have a folder project that has 2 files and one folder. System folder, index.php, style.css.
I have only one file in view folder i.e. main_view.php. I am using this view file for my all controllers. How will i link my css file in this main_view.php?

I tried different thngs but not succeeded yet. Sad


css file and methods - El Forum - 06-07-2010

[eluser]mddd[/eluser]
I'm not sure I understand the problem.. Just write
Code:
link href="/style.css" type="text/css" /
and it should work fine.


css file and methods - El Forum - 06-07-2010

[eluser]Ivar89[/eluser]
Code:
<link rel="stylesheet" href="/style.css" type="text/css" />
should work I think...

EDIT: sorry mdddTongue, I forgot to click post, didn't see you posted already.


css file and methods - El Forum - 06-07-2010

[eluser]CIfan[/eluser]
[quote author="mddd" date="1275932706"]I'm not sure I understand the problem.. Just write
Code:
link href="/style.css" type="text/css" /
and it should work fine.[/quote]
Or put a base href in your header: http://www.w3schools.com/TAGS/tag_base.asp


css file and methods - El Forum - 06-07-2010

[eluser]mysterious[/eluser]
<code> &lt;link rel="stylesheet" href="/style.css" type="text/css" /&gt; </code>

It does not work this way..