Welcome Guest, Not a member yet? Register   Sign In
Dynamic Css
#1

[eluser]vile[/eluser]
Hi,

Im working on dynamic. How can i pass data to css?

here's my code:
controller:
Code:
class Theme extends Controller {

    function theme()
    {
        parent::Controller();
        $this->load->helper('url');
    }
    
    function index()
    {
        $data['bgcolor'] = "#ff0000";  
        $this->load->view('header',$data);
            
    }
}

view
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Theme&lt;/title&gt;
&lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url()?&gt;/css/default.php?&lt;?php echo time();?&gt;"&gt;
&lt;/head&gt;
&lt;body&gt;
TEsting
  
&lt;/body&gt;
&lt;/html&gt;


css
Code:
&lt;?php
header('Content-type: text/css');
?&gt;
body
{
    background-color: &lt;?php echo $bgcolor;?&gt;;
}


i cant get the value of bgcolor in default.php Sad
#2

[eluser]jedd[/eluser]
Do you really need to faff with the in situ style sheet, or are you just trying to have multiple style sheets - selecting them based on current theme, say?

I do the latter - and in my main view file I have this code:
Code:
echo "\n". link_tag('assets/stylesheets/COMMON.css');
echo "\n". link_tag('assets/stylesheets/'. $theme .'.css');

Remember the C in CSS stands for cascading.
#3

[eluser]vile[/eluser]
ummm what im trying to do is to get style from the database..
i have default template. but i want to make it dynamic. customizable Big Grin
#4

[eluser]Jamie Rumbelow[/eluser]
Then make a Stylesheets controller with various methods for the different stylesheets. Put the CSS in a view along with your PHP and remember to put

Code:
$this->output->set_header('Content-type: text/css');

In your controller somewhere. Then just link to it how you would any other action! You've got full access to the CI masterobject and PHP within your CSS stylesheets, and it doesn't require any nasty hacks.

Jamie
#5

[eluser]Unknown[/eluser]
Has anyone looked into integrating CSScaffold into codeigniter?
http://wiki.github.com/anthonyshort/csscaffold

If I get some time one day i'll give it a shot myself...
#6

[eluser]CI Coder[/eluser]
First of all I think you have "/css/default.php" outside of CI's directory structure. If that's the case you cannot do what you want. What you want to do is done by making the stylesheet a CI view and passing all the data to it as normal. So, if you want to try it here it is:

Make a view: stylesheet.php that contains this: (just like a css file)

Code:
body
{
    background-color: &lt;?php echo $bgcolor;?&gt;;
}
in the view of your page
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Theme&lt;/title&gt;
&lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url(); ?&gt;theme"&gt;
&lt;/head&gt;
&lt;body&gt;
TEsting
  
&lt;/body&gt;
&lt;/html&gt;
then in your Theme::index() function do this:
Code:
function index()
    {
        $data['bgcolor'] = "#ff0000";  
        $styles = $this->load->view('stylesheet',$data, TRUE);
        header('Content-type: text/css');
        echo $styles;
    }

It's a good idea to do it this way. It'll give you a lot of options later in the design.

Good luck.
#7

[eluser]vile[/eluser]
[quote author="CI Coder" date="1259123157"]First of all I think you have "/css/default.php" outside of CI's directory structure. If that's the case you cannot do what you want. What you want to do is done by making the stylesheet a CI view and passing all the data to it as normal. So, if you want to try it here it is:

Make a view: stylesheet.php that contains this: (just like a css file)

Code:
body
{
    background-color: &lt;?php echo $bgcolor;?&gt;;
}
in the view of your page
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Theme&lt;/title&gt;
&lt;link rel="stylesheet" type="text/css" href="&lt;?php echo base_url(); ?&gt;theme"&gt;
&lt;/head&gt;
&lt;body&gt;
TEsting
  
&lt;/body&gt;
&lt;/html&gt;
then in your Theme::index() function do this:
Code:
function index()
    {
        $data['bgcolor'] = "#ff0000";  
        $styles = $this->load->view('stylesheet',$data, TRUE);
        header('Content-type: text/css');
        echo $styles;
    }

It's a good idea to do it this way. It'll give you a lot of options later in the design.

Good luck.[/quote]

Thanks! it worked. Big Grin
i used &lt;style&gt;&lt;/style> instead of header().




Theme © iAndrew 2016 - Forum software by © MyBB