1

I am developing a CMS so I need each respective site's global css file to be stored in a database and loaded. I have a controller called util and the method is called sitecss. So my main wrapper view looks like this:

<link rel="stylesheet" type="text/css" href="/util/sitecss">

The css loads, but has no effect. If I view source on the page, and then click on the link, I can see the css just fine. So I know it is being loaded. Is it something about it not coming from a file? Or perhaps the browser assuming it is cached when it is not?

If I make a static file and change to above to

<link rel="stylesheet" type="text/css" href="/css/site.css">

everything works just fine. If I do this in .NET with a handler ashx file, it works fine. It is in php with Codeigniter that I am having the problem. I know someone will ask "why don't you just make static files?" and the answer is, it is not practical for this application. This is for thousands of sites with very rapid deployment.

Thanks in advance!

EDIT:

My controller method looks like this:

function sitecss() {
    $cssdata = $this->cmsutils->loadCss($this->session->userdata('sitecss'));
    echo $cssdata;
}

So can I just echo a mime-type first? It doesn't seem like this will work as I am making this call within the

1
  • 1
    Have you tried returning the content-type of the dynamically generated CSS file as text/css? It may be sending it as a different type that the browser does not interpret as CSS, and then ignores. Commented Jun 20, 2012 at 23:48

2 Answers 2

1

write a caching library to pull from the database and create a css file in a cache folder.

You will need:

Library Class

  • Interact with the css and create a form to perform CRUD
  • handle cache file monitoring CRUD (every hour or, even on every C,U,D of the form)
  • Inject the stylesheet cache file into the DOM view

Model

  • Interact with the database and perform CRUD operations
  • return data to the Controller for creating the cache file

View

  • parse out the values into a css stylesheet file format

The other option is to define a mime type with a controller and just load a view with the stylesheet properly formatted. No writing to the filesystem or anything.. Add a .css extension to the end of the URI and call it good...


I do this exact same thing for an app that I just released. I have a form in a view on the admin section of the app that has specified textfields. The user inputs hexadecimal color codes and then it saves/updates the data in the database. The library then creates a cached css file that is referenced in the header view. We did this to eliminate the need for us to add a .gitignore file in a special directory when we deploy the app to several clients.

Sign up to request clarification or add additional context in comments.

Comments

1

Could you just load the css by passing it to the view from the controller and then echo it in the header somewhere or somewhere else in the view like this:

controller:

$data['css_rules'] = $this->yourcssmodel->get_css_rules_function();
$this->load->view('yadayadayada, $data);

view: (likely in header)

?>
<style>
<?echo $css_rules;?>
</style>

6 Comments

The problem with this is that it will then be sent on every request, rather than taking advantage of the user's browser cache.
hmmmmm. I may have misunderstood the question then. I thought part of the issue was he didn't want the css to cache.
Cacheing is irrelevant. I could display it as you suggest but I prefer not to. I'd rather do the load. What I can't understand is that a browser makes a request to the server, then the server sends some text back. How does it know the difference between a bunch of text loaded from a database and a bunch of text loaded form a file. They should be the same thing. I do this in .NET all the time with handlers.
@DougWolfgram It's probably not the correct content-type or MIME type being sent back with the "bunch of text." It needs to be text/css or the browser will mostly likely ignore it. You can set the header in PHP before echoing anything on to the page, and see if that does the trick.
yeah it probably is the mime type like jblasco says. what exactly does your sitecss function render/do? could you post the code if no one elses suggestions do the trick?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.