SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    echo domain name in css stylesheet

    hi all

    i have a comman variable on top of every page
    PHP Code:
    $domain "http://localhost/myfolder"
    and i m writing addresses of css files and images as
    PHP Code:
    <link rel="stylesheet" type="text/css" href="<? echo $domain ?>/styles.css" />
    But how would i call $domain variable in my stylesheet

    Code:
    body{font-size:12px; background:url(../images/bg.gif) repeat-x;}
    vineet

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,996
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Short answer: can't be done.

    Long answer: using a bit of black magic you can add variables to your CSS Files by asking Apache to redirect any stylesheet to a special PHP script which will open the stylesheet, find and replace any user-defined variables, then serve up the parsed content will be displayed as pure CSS.

    Here's a tutorial that describes how to do exactly that: http://net.tutsplus.com/tutorials/ht...our-css-files/

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Similar could be done using JS I suppose.
    PHP Code:
    <?php
    $domain 
    "mywebsite1";
    ?>

    // then elsewhere, building up some html/JS output

    $('#dvBackGround').css('background','path/to/image/<?php echo $domain?>.jpg');

    // back on with the page

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2011
    Location
    Novi Sad
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You cloud create additional file called custom.css and place it in a folder relative to your PHP $domain var path. Inside it, you can put your CSS and always be sure what path to use in url(), like in background CSS property.

  5. #5
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    thank pullo

    thats a long article. i will spend time learning it

    vineet

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,215
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Short answer: can't be done.
    I might be wrong, but I would think so long as you return a mime type header of text/css the browser will render it as a CSS instruction.

    So the following could be done (in theory)

    Variable setting
    PHP Code:
    $domain "http://localhost/myfolder"
    HTML markup
    PHP Code:
    <link rel="stylesheet" type="text/css" href="<? echo $domain ?>/stylesheet.php?domain=<?php echo $domain?>" />
    Filename: stylesheet.php
    PHP Code:
    <?php 
      $domain 
    $_REQUEST['domain']; // Add validation to make sure someone doesn't redirect their to their own domain
      
    header('Content-Type: text/css;');
    ?>
    body{font-size:12px; background:url(<?php echo $domain?>/images/bg.gif) repeat-x;}

  7. #7
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks cpradio

    thats short and works for me

    exactly what i wanted.

    vineet

  8. #8
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,996
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by cpradio View Post
    I would think so long as you return a mime type header of text/css the browser will render it as a CSS instruction.
    +1 for proving me wrong.
    That's a neat trick!

  9. #9
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,880
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by vinpkl View Post
    hi all

    i have a comman variable on top of every page
    PHP Code:
    $domain "http://localhost/myfolder"
    and i m writing addresses of css files and images as
    PHP Code:
    <link rel="stylesheet" type="text/css" href="<? echo $domain ?>/styles.css" />
    But how would i call $domain variable in my stylesheet

    Code:
    body{font-size:12px; background:url(../images/bg.gif) repeat-x;}
    vineet

    Couple of points maybe worth noting:

    I am curious to know why you want a $domain variable at the top of every page?
    I envisage "file not found " problems will arise when the files are uploaded to www.your-online-site.com.

    The way I get round this is to test and see if platform is localhost and to define relevant constants.

    // Typical file
    PHP Code:
    <?php 

      
    // $domain = "http://localhost/myfolder";   
      
    require '/your-include-path/constants.php'

      
    <link rel="stylesheet" type="text/css" href="<?php echo DOMAIN ?>/styles.css" />

    // constants.php
    PHP Code:
    <?  
      defined
    ('LOCALHOST') ?: define('localhost''localhost' == $_SERVER['SERVER_NAME']);

      if (
    LOCALHOST)
      {
        
    defined('DOMAIN') ?: define('DOMAIN''http://localhost/');
        
    defined('P_IMGS') ?: define('P_IMGS''http://localhost/path-to-images/');
      }
      else
      {
        
    defined('DOMAIN') ?: define('DOMAIN''http://your-online-site.com/');
        
    defined('P_IMGS') ?: define('P_IMGS''http://your-online-site.com/path-to-images/');
      }
    <hr />

    But a far easier solution is to create a localhost/assets-folder/ which is duplicated online. This method does not require any constants or variables...

    Test the following code to ensure that PHP prepends the URL on both localhost and online platforms.

    # typical files
    PHP Code:
    <?php

      
    <link rel="stylesheet" type="text/css" href="/assets-folder/css/styles.css" /> 

    // stylesheet
      
    body{font-size:12pxbackground:url( /assets-folder/images/bg.gif repeat-x;}
    Last edited by John_Betong; Sep 19, 2012 at 22:23. Reason: replaced http:// with www.
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  10. #10
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,880
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    @cpradio, @vinpkl, @Pullo

    I would think so long as you return a mime type header of text/css the browser will render it as a CSS instruction.
    I used the above method for developing but unfortunately www.validation.w3.org does not recognise the .php extension.

    To get round this I view the source using the FireFox browser then click on the stylesheet.PHP file and the source appears in another window. Source then copied, pasted and saved to stylesheet.css
    Last edited by John_Betong; Sep 19, 2012 at 22:43. Reason: spellling: not my fortay
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,182
    Mentioned
    16 Post(s)
    Tagged
    4 Thread(s)
    Validation is only a guide. Something like this won't even matter whether or not it pases validation. Unless you just can't live without da badge.
    The only code I hate more than my own is everyone else's.

  12. #12
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,880
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by oddz View Post
    Validation is only a guide. Something like this won't even matter whether or not it pases validation. Unless you just can't live without da badge.
    I prefer to have a site validated because there are usually fewer visual browser discrepancies.

    Saving the generated PHP stylesheet source to a stylesheet.css file eliminates this problem and being ready for the next hurdle

    I think "da badge" is off-putting and frequently when testing validation links, numerous glaring errors are shown.
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014

  13. #13
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,215
    Mentioned
    153 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by John_Betong View Post
    I used the above method for developing but unfortunately www.validation.w3.org does not recognise the .php extension.

    To get round this I view the source using the FireFox browser then click on the stylesheet.PHP file and the source appears in another window. Source then copied, pasted and saved to stylesheet.css
    I did not know that. That is good information to have. I'm sure there are ways that can be worked around it (if need be), but it is something to keep in mind that you will have to do a workaround to validate your stylesheets.

  14. #14
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why are you using the domain variable? Start the url with a forward slash and it uses the root anyway.

    http://localhost/myfolder === /myfolder
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  15. #15
    SitePoint Guru
    Join Date
    Nov 2008
    Posts
    622
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    yes, i also tried to validate it

    but it didnt get validated

    vineet

  16. #16
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,880
    Mentioned
    74 Post(s)
    Tagged
    6 Thread(s)
    Quote Originally Posted by vinpkl View Post
    yes, i also tried to validate it

    but it didnt get validated

    vineet
    Please supply a link to the site.
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Wolfshade on earning Member of the Month for August 2014


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •