Welcome Guest, Not a member yet? Register   Sign In
php config variable into css files
#1

[eluser]Byzs[/eluser]
Hello to everybody, this is my first time in the forums and couldn’t look for a solution here or in the internet.

So, first i was looking for a solution on how to insert php code into my css file, i found this:

1) Name your .css file as .php file
2) At the top of the file specify the mime type as a text/css like this:

Code:
<?php
    $mtype = "text/css";
    header("Content-Type: $mtype");
?>

3) load your style.css file as style.php from your view and done !

It works like charms, but i have a little problem when i’am try to load a config variable into a css section, like this:

Code:
#form_container
{
background:#fff;

margin:0 auto;
text-align:left;
width:<?php echo $this->config->item('width');?>px;
}

Everything goes wrong and the style is not loaded at all.

Any ideas would help !

Sorry for my bad english.
#2

[eluser]PhilTem[/eluser]
That's probably because it won't work this way. If you include your stylesheet with a

Code:
<link rel="stylesheet" type="text/css" href="/base.css" />

Then it will be accessed by your browser as a "regular" page meaning it will be processed as it is. However, if you make it a .php file to integrate your config items, you may want to the link-rel tag point to a controller/method combination that will return the css-code. Otherwise it (the css-php file) won't know what $this->config is.

Hope, this was a good hint towards the right direction. If not just tell us Wink
#3

[eluser]jmadsen[/eluser]
First, are you wrapping your style with
Code:
[removed][removed]
tags?

essentially what you seem to be doing is loading your css styles as a string & outputting to the page. However, you said that part is working, so perhaps you've handled it.

Second, can you post the error you are getting - "everything goes wrong" is difficult to debug. If you can post a copy of your file code - perhaps remove a bunch of the css so it isn't too big - that will also help us.
#4

[eluser]Matalina[/eluser]
You will need to create a css controller and a css method and then link that in like phil set

ie:

Code:
class Css extends CI_Controller {

  public function default() {
    $mtype = "text/css";
    header("Content-Type: $mtype");
   ?>
     #form_container
     {
       background:#fff;

       margin:0 auto;
       text-align:left;
       width:<?php echo $this->config->item('width');?>px;
     }
   <?php
  }
}

Your link would be

Code:
<link rel="stylesheet" type="text/css" href="<?php echo site_url('css/default');?>" />
#5

[eluser]PhilTem[/eluser]
@Matalina just posted, what I suggested as a solution. But she did it with a code sample. However, you really never want to output code in your controller especially since this output is not cached neither read from the ob_buffer. That's actually what you got the views in a MVC framework for. And it's easier to change the code if there are any errors Wink
#6

[eluser]Byzs[/eluser]
Thanks to everyone for the fast replys.

jmadsen:

I don't know what [removed] tag is, i don't know mucho of css, just the basic to change some variables and get what i want (anyway the answer is NO, since there isn't any [removed] tag).

For the "evrything goes wrong" is that actually the css is not loaded, i got no error messages, just the style is not loaded.

Also, if i try this:

Code:
<?php
  $width=768;
?>

#form_container
{
background:#fff;

margin:0 auto;
text-align:left;
width:<?php echo $width;?>px;
}

It works just fine, the style is loaded...but what I really want is a "config" variable that i could change from any controller I want.


I will try what Matalina said and then post some solution.

Thanks to everybody for the help !
#7

[eluser]skunkbad[/eluser]
You could use mod rewrite and rewrite the request for the css file to a php file.




Theme © iAndrew 2016 - Forum software by © MyBB