Lars282 Posted October 18, 2012 Share Posted October 18, 2012 Hey, I want to use PHP in my CSS file. The only way I could make it work is to add my style.php including header("Content-type: text/css") as a template, and then include this page as the stylesheet in my other templates. While this does offer advantages (style can be easily customised from the PW admin), is there a nicer way of doing this? Thanks, Lars Link to comment Share on other sites More sharing options...
apeisa Posted October 18, 2012 Share Posted October 18, 2012 I usually just use <style> tags when in need of adding dynamic css. The amount of dynamic code is so small, that I think it is most flexible solution. Link to comment Share on other sites More sharing options...
teppo Posted October 18, 2012 Share Posted October 18, 2012 Lars: I'm not completely sure that I understand your question, but the way you've done it (php-file with css-header) is, as far as I know, pretty much the cleanest way to do this. Is there a reason not to use that method? I'm sure you've tried Googling this already, but this solution does seem rather common: http://css-tricks.co...ables-with-php/, http://stackoverflow...-file-extension, http://net.tutsplus....under-the-hood/ and so on. I'd recommend you stick with it. It's pretty enough. Link to comment Share on other sites More sharing options...
Lars282 Posted October 18, 2012 Author Share Posted October 18, 2012 teppo: I mean the fact that I cannot simply put the .php file in the styles folder and include it in my templates as PW prohibits execution of php files accessed directly. The style.php needs to be added as a page having the style.php template in PW admin, and only then can be included as include($page->get(template=style)->path). What I find would prefer is not having my stylesheet as a page in the page overview. Is there a way? Link to comment Share on other sites More sharing options...
teppo Posted October 18, 2012 Share Posted October 18, 2012 I'm afraid that I'm still a bit confused about your situation and thus my answer might not be completely valid. I don't see any problems in including a .php file with include() even if it's not related to any template -- I'm actually doing that a lot when building sites with PW. You can't access a .php file inside /site/ or /sire/ with direct URL (and thus you cannot use them directly from <link> tags etc.) but include() within a page (/ template) is completely different thing. Anyway, there are three other options I can think of right now: 1. Put your style.php in the root of your website, outside /site/ folder. This way PW (or actually mod_rewrite) shouldn't have a problem with it. 2. Same as option 1. but with a twist: add style.php to site root and from there run include('./site/style-directory/style.php'). Only thing this would achieve would be that your style.php would actually live in your styles folder. Not exactly pretty, but it should work. 3. Add new rewrite rule to your .htaccess to allow direct access to file / files in your styles folder. This option also sounds like a hack and I wouldn't really suggest doing it, though. I'd go with having style as a normal PW page; example.com/style/ or something like that. It's pretty much the prettiest option I can think of right now and would make adding new user-definable CSS settings a breeze. 1 Link to comment Share on other sites More sharing options...
Lars282 Posted October 20, 2012 Author Share Posted October 20, 2012 Thanks for your extensive reply! but include() within a page (/ template) is completely different thing Not sure I understand what you mean. Currently, I have <link rel="stylesheet" media="all" href="<?=$pages->get("template=style")->path?>" type="text/css" /> Do you suggest something like <style><?=include($config->urls->templates/styles/style.php)?></style>? Link to comment Share on other sites More sharing options...
Soma Posted October 20, 2012 Share Posted October 20, 2012 Or just <link rel="stylesheet" media="all" href="<?php include($config->urls->root . "styles/style.php")?>" type="text/css" /> Edit: Sorry, again... not include but: <link rel="stylesheet" type="text/css" href="<?php echo $config->urls->root?>styles/styles.php" /> Put styles in the root /styles/styles.php And there you could do : <?php header("Content-type: text/css"); $bgcolor = '#000'; ?> body{ background-color: <?=bgcolor?>; } Or yeah the <style> tag could also be handy and an easy way to do this. 1 Link to comment Share on other sites More sharing options...
Lars282 Posted October 20, 2012 Author Share Posted October 20, 2012 Soma, this is exactly what i tried to do and what I wish would work, but unfortunately, this does not work. The result is simply an unformatted page, as if the stylesheet never was included. I assumed that this was due to the fact the PW prohibits access to the styles.php? Does this work for you? If so, I will check again and see, but I couldn't get this to work like this for me before. Link to comment Share on other sites More sharing options...
Soma Posted October 20, 2012 Share Posted October 20, 2012 Well it works for me, I don't see why this shouldn't work. PW doesn't prohibit acces to php if you out it in the root. Link to comment Share on other sites More sharing options...
teppo Posted October 20, 2012 Share Posted October 20, 2012 Not sure I understand what you mean. Currently, I have <link rel="stylesheet" media="all" href="<?=$pages->get("template=style")->path?>" type="text/css" /> Do you suggest something like <style><?=include($config->urls->templates/styles/style.php)?></style>? Minor misunderstanding: when you said "I cannot simply put the .php file in the styles folder and include it in my templates", I thought you meant "include" literally -- using PHP include(), that is. Sorry, my mistake. Anyway, including content of style.php inside <style> tag (instead of linking to it with <link> tag) definitely is one way to do this, but not what I meant. That way user would have to download those styles again for each page they view instead of letting the browser cache the stylesheet file on first load. What Soma was explaining above is pretty much exactly what I meant with option 1: put your style.php into root or a folder in it, at the same level with /site/ and /wire/ directories. Let's imagine that your site lived in /example.com/ directory: /example.com style.php /site /wire This way you can link it to any other template with <link> tag. With option 2. above I meant a structure like this: /example.com style.php => only things you'd put in this file would be CSS header and "include(./site/templates/style.php);" /wire /site /templates style.php This second method is no "better" than first method, mostly just more complicated. It'd let you put your style.php inside templates folder, though, if that's what you want; I myself prefer to keep all template-specific content there, so I always know where to find them when making changes. This method isn't pretty but it does allow for style.php to live inside templates directory and still get referenced with direct URL. By the way, if I may ask, what kind of stuff are you using PHP for in your stylesheet? I'm just trying to wrap my head around exactly why you'd wish to use PHP in your stylesheet but not have it as a PW page -- wouldn't it be nice to be able to, for an example, manage possible variables directly from PW? 2 Link to comment Share on other sites More sharing options...
Lars282 Posted October 24, 2012 Author Share Posted October 24, 2012 Thanks! The only reason I don't want the stylesheet as PW page is that it makes things 'messier' for whoever uses the admin to add/edit pages. Is there a way of this only being visible in the admin to users with a certain role, even when it is viewable as a guest? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now