views:

3880

answers:

4

I am developing an custom API for a web solution and I am using the MVC design pattern. I have a modules folder so that I can swap in and out modules and also work on sections without disrupting working tested code. My only issue now is that I want to load CSS anywhere and have my application properly import the css file in the head tag. I know CodeIgniter does this but I'm not sure how.

Using PHP, how do I load in a CSS file anywhere and then have the code properly import the css within the head tags like CodeIgniter does?

Thanks in advance.

+1  A: 
David Dorward
+1  A: 

you can do the same as i do, in your view file do the following

<link rel="stylesheet" href="{pathToApp}/styles/main.css" type="text/css" media="screen" />

(after creating a styles folder in the application directory) then in your controller, pass this:

$path = '../system/application';

into an array and send the array as the second parameter, if you are using load->view to load your view's you need to change {pathToApp} to $array['path'] (change $array to whatever you called it). If you are using CodeIgniter's built in templating system then you are all set. At least with this way you won't need to change the absolute URL when you migrate your site.

Marc Towler
+2  A: 

Your question is a little unclear to me, but I'll do my best to help. Are you simply wondering how to include a CSS file in your Views? If so, simply use the following:

<style> @import url('/css/styles.css'); </style>

If your CSS folder is at the root of your CodeIgniter project, you could do something like this using CodeIgniter's base_url() function:

<style> @import url('<?=base_url()?>/css/styles.css'); </style>

It will ensure your pages stay portable and have the correct absolute URL. Hope this helps! If not, try being a little more specific in your question

Colin
+3  A: 

You can load several views at once, or views inside other views.

So in this case I recomend you to create one header view where you load all css and js files

example:

<html>
<head>
 <meta http-equiv="content-type" content="text/html;charset=UTF-8">
 <link rel="stylesheet" href="<?php echo base_url();?>css/moorainbow.css" type="text/css" media="screen"/>
    </head>
    <body>

And call it like:

$this->load->view('header');
$this->load->view('view1');
$this->load->view('view2');

This way you can control the files (css+js+etc) you load in just one file.

Regrads,
Pedro
@pcamacho

Pedro
base_url() is the way to go.
andyk