Welcome Guest, Not a member yet? Register   Sign In
Dynamic CSS -> how to get conditional data into style.php
#1

[eluser]codex[/eluser]
Dynamic CSS is possible by putting the correct header in a php file:

Code:
<?php header("Content-type: text/css")?>

That works fine, but now it's time to get some conditions/vars into the file. I'm calling a function in my base controller that gets all the site settings and I would like to get data from that into style.php, but I can't get it to work. Code below doesn't work. Any ideas how to get the data in?

function call in controller
Code:
$data['settings'] = $this->system->getSettings('site');

style.php
Code:
<?php header("Content-type: text/css")?>
html, body {
    font: 0.9em Arial, Helvetica, sans-serif;  
    color: #000;
    background: <?=$settings['bg']?>;
}
#2

[eluser]TheFuzzy0ne[/eluser]
[quote author="codex" date="1209236176"]Dynamic CSS is possible by putting the correct header in a php file:

Code:
<?php header("Content-type: text/css")?>

That works fine, but now it's time to get some conditions/vars into the file. I'm calling a function in my base controller that gets all the site settings and I would like to get data from that into style.php, but I can't get it to work. Code below doesn't work. Any ideas how to get the data in?

function call in controller
Code:
$data['settings'] = $this->system->getSettings('site');

style.php
Code:
<?php header("Content-type: text/css")?>
html, body {
    font: 0.9em Arial, Helvetica, sans-serif;  
    color: #000;
    background: <?=$settings['bg']?>;
}
[/quote]

Sounds to me like you might need another controller specifically for your CSS calls. Then it would just be a case of including the file using a link, with the right parameters.

For example:

Code:
<link rel="stylesheet" type="text/css" href="index.php/css_controller/theme1">

or

Code:
<link rel="stylesheet" type="text/css" href="index.php/css_controller/theme2">

etc...

I hope this answers your question.
#3

[eluser]codex[/eluser]
[quote author="TheFuzzy0ne" date="1209238988"][quote author="codex" date="1209236176"]Dynamic CSS is possible by putting the correct header in a php file:

Code:
<?php header("Content-type: text/css")?>

That works fine, but now it's time to get some conditions/vars into the file. I'm calling a function in my base controller that gets all the site settings and I would like to get data from that into style.php, but I can't get it to work. Code below doesn't work. Any ideas how to get the data in?

function call in controller
Code:
$data['settings'] = $this->system->getSettings('site');

style.php
Code:
<?php header("Content-type: text/css")?>
html, body {
    font: 0.9em Arial, Helvetica, sans-serif;  
    color: #000;
    background: <?=$settings['bg']?>;
}
[/quote]

Sounds to me like you might need another controller specifically for your CSS calls. Then it would just be a case of including the file using a link, with the right parameters.

For example:

Code:
<link rel="stylesheet" type="text/css" href="index.php/css_controller/theme1">

or

Code:
<link rel="stylesheet" type="text/css" href="index.php/css_controller/theme2">

etc...

I hope this answers your question.[/quote]

I'm not sure on how you would tackle this in a special controller. Perhaps an example?

EDIT: Are you talking this (post #12)? http://ellislab.com/forums/viewthread/62286/
#4

[eluser]TheFuzzy0ne[/eluser]
[quote author="codex" date="1209240093"]

I'm not sure on how you would tackle this in a special controller. Perhaps an example?
[/quote]

I could, but I first I need to make sure I understand what you are trying to achieve. I think I underrated the importance of "dynamic". My suggestion would work by, (in theory), passing back "static" CSS pages (which, with more work could be totally dynamic).

It should be possible to dynamically generate the URI for the <link> in the <head>, which the browser will receive, and request the CSS page for. When the request hits the server, it will then send it to your new CSS controller, which will process the request. The CSS can then be handed back either dynamically or statically, according to the parameters passed to it via the request (i.e theme1 or theme2/blue etc...).

The only problem I can see with this idea is that if any headers get sent before your CSS headers, it will most likely cause problems, but if you don't use a view, I don't believe any headers will be sent (although I'm really not sure).

The easiest way would probably be to try it. It shouldn't take long to get a CSS/theme controller setup that will send back static data. If that works, you can make it dynamic - again, very simply.

The idea is quite simplistic, but I find those ideas often serve me well. I'd be happy to try and write you a simple example once I'm sure I know exactly what you want.

[quote author="codex" date="1209240093"]
EDIT: Are you talking this (post #12)? http://ellislab.com/forums/viewthread/62286/[/quote]

No. I'd not actually seen that post until just now. I think that they are dynamically generating their CSS files using something like a template parser, which replaces variables in the file it parses.

It's entirely up to you how you do it. I believe my suggestion may be simpler.

Where are you storing the CSS? In a database? XML, text file? I can't seem to get the full picture of what you want to acheive. The basic principle of my suggestion would pretty much work in the same way. How you generate the actual data output is entirely up to you.
#5

[eluser]codex[/eluser]
Well, in this specific case it's only about changing the body attributes. A customer is requesting that he's able to show different background images (via the cms I'm working on). I know it can easily be done with
Code:
<style type="text/css">
body {
  background: url(path/to/image);
}
</style>
in the html, but I was just wondering if it would be possible to feed a css file data retrieved from a database. That way I could incorporate more settings done in the cms regarding style. If you still can make an example, please do. I'm curious to know how this might be tackled. Maybe there's a usable elegant solution. :-)
#6

[eluser]wiredesignz[/eluser]
I was impressed by the Asset (Media) Controller feature in Kohana. It works something like this:

Directory structure for application/views:
Code:
views/
    css/
        stylesheet.css

In your View header:
Code:
<link rel="stylesheet" type="text/css" href="<?=site_url('assets/css/stylesheet.css');?>" />

Using an Assets Controller in application/controllers
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Asset Controller
**/
class Assets extends Controller
{
    function Assets()
    {
        parent::Controller();
    }
    
    function css($css)
    {
        $this->load->view('css/'.$css);
    }
}
You can see the controller decides which stylesheet to display, so you can add code here to swap styles based on some other criteria. Your stylesheet is also parsed by the View loader so you could add PHP to your stylesheet.

You could do the same with assets/images and assets/javascript.




Theme © iAndrew 2016 - Forum software by © MyBB