Welcome Guest, Not a member yet? Register   Sign In
SOLVED: CSS background: url( to a variable relative path
#1

[eluser]ThijssjihT[/eluser]
CSS:
Code:
#bg {
width:1024px;
background:url(images/bg.gif) left repeat-y;
margin: 0px auto; }

view:
Code:
<img src="&lt;?=$include?&gt;/images/img1.jpg">

Images called from the view as above are being displayed. Images called from the css as above are not. $include is a variable, so I can't set a complete url in my css. How can I link to images in my css, depending on the path stored in $include?

Sorry for my bad English.
#2

[eluser]theprodigy[/eluser]
I've handled situations like this before by creating a controller to handle the css. This way, your css is built using php (and variables), and the src in the link tag is just the url to that css controller.

The way I handled dynamic data is store it in the database, with a user_id foreign key, and grabbed the data needed using the user_id from session

Example:
Code:
class Css extends Controller
{
    function __construct()
    {
        parent::__construct();
    }
                              
    function index()
    {
        $data = $this->css_model->get_data($this->session->userdata('user_id'));
        
        $this->output->set_header("Content-type: text/css");
        
        $this->load->view('css');
    }
}
Then, in your css view, use the variables that you set in $data just as you would with a normal view, just build it as you would a normal css file.

Your link tag will look something like:
Code:
echo link_tag('css');
//or if manually writing it
&lt;link href="http://site.com/css" rel="stylesheet" type="text/css" /&gt;

The benefit to using a controller is that you can create more functions for different css sheets. If you want a homepage specific css "file", create a homepage function, and then call it.

Code:
echo link_tag('css/homepage');
//or if manually writing it
&lt;link href="http://site.com/css/homepage" rel="stylesheet" type="text/css" /&gt;
#3

[eluser]ThijssjihT[/eluser]
I don't understand it now, but I will after reading it a couple of times. It might take a few tries, but I will work that out. Thanks a lot for your help! :-)
#4

[eluser]theprodigy[/eluser]
let me know if you want me to explain it (or parts of it). I'd be glad to help further.
#5

[eluser]Unknown[/eluser]
Hi theprodigy,

So happy to find this post! ...to which I'm trying hard to connect...

Could you be more specific about what you suggest above?

Where do you exactly enter the "class Css extends Controller" you suggest in the first place? I guess this is the php part. In a blog theme for instance, would that typically be in the function.php?

It's not clear to me what value exactly this allows to extract and how the value can then be used as a variable.

I'm running severals blogs in the WordPress multi-sites mode. The issue I have with the theme I use is that it loads the background image from the main image directory of the theme, not from the sub-blog specific image directory, which means I currently can only load the same background for all my blogs.

The background image is defined in the main css file of the theme. I would therefore like to change the ccs file to include the variable into the path to the background image so as each sub-blog can have its own individual background.

Currently, the ccs part reads:
body {
font: 11px Verdana;
color: #FFFFFF;
background: #151515 url('../images/001/body_BG.jpg') top left no-repeat;
}

The sub_blog_ID should be the variable part of the filename or subdirectory.

1) What do I have to write in which php file to extract a sub_blog_ID?
2) Then how to I exactly write the ccs part to include the sub_blog_ID variable?

Thanks in advance for your help!

Trevor




Theme © iAndrew 2016 - Forum software by © MyBB