Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Creating dynamic CSS using .htaccess, css file and php

Post new topic   Reply to topic    Smarty Forum Index -> Article Discussions
View previous topic :: View next topic  
Author Message
Smarty Elite

Joined: 19 Apr 2007
Posts: 420
Location: Kathmandu, Nepal

PostPosted: Thu Oct 15, 2009 8:28 am    Post subject: Creating dynamic CSS using .htaccess, css file and php Reply with quote

Wondering about how to work with Dynamic CSS? It is possible in numerous ways. And here, I am explaining about how to do URL Rewriting and use PHP variables to write the dynamic css. This article is not related to core Smarty, but can aid more advantages in producing user specific dynamic css, for your project.

Yet, it is supporting external css files, which is the main aim to demonstrate here.

You can make it dynamic per session, per user or just randomly dynamic.

The contents of .htaccess in your css folder should be:

<IfModule mod_rewrite.c>
# Rewrite example: main.css => index.php?css=main
RewriteEngine On
RewriteRule ^$ index.php [L]
RewriteRule (.*).css index.php?css=$1  [QSA,L]

Now, anytime, you access .css file in this folder, index.php file will be activated, which can then: read the dynamic values for some css attributes and place them in the css output.

A quick trick on index.php is (more details are here, for downloading working codes):

# Set some variables, use config file, or user specific database configurations.
$css = array();
$css['body']['color'] = '000033';
$css['body']['bgcolor'] = 'CCCCCC';
$css['h1']['color'] = '003399';
$css['a']['color'] = '990000';

# Validates and maintains a copy of default css file that works.
# If dynamic css file is verified, use it.
$css_file = isset($_GET['css'])?(string)$_GET['css'].'.css':'default.css';
$css_file = (file_exists($css_file))?$css_file:'default.css';

# Now begin writing your dynamic css using your variables.
# Below code is a true css content.

All you have to do is maintain $css_file correctly, that can utilize the dynamic variables.

Must see for:
a demo – working copy.
More details and download: Help Self discussion.

Rejoice again. Thanks and let me know your feedback.
Back to top
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Article Discussions All times are GMT
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP