Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating CSS using ASP

Formatting

Generating CSS using ASP

by  Kavius  Posted    (Edited  )
What this does is create a css script dynamically based on a parameter. This could be used to allow users to define or choose their own color scheme based on personal preference or different companies, using the same interface could be given different color layouts.

The styles can then be placed into a relational database for later use.

A friend of mine (GumB on tek-tips) has also used this concept as an interface between a COBOL front-end and an oracle database. The front-end was built using COBOL and requires a flat file as input, wanting to upgrade the back-end for ease of processing, but having a perfectly good front-end that the users were already familiar with, he built an interface between the front-end and the Oracle database. To be fair, he used JSP since he is a Java nut case, but to do something like this ANY server side scripting tool will do (ASP, JSP, CGI,...).

Step 1: Create Table
This could actually be three or four tables, but for this example:
Code:
create table cssClass(
  style as varchar(20),
  class as varchar(20),
  precedence as int
)

insert into css values('DEFAULT','a',1)
insert into css values('DEFAULT','a:active',2)
insert into css values('DEFAULT','a:hover',2)

create table cssProp(
  style as varchar(20),
  class as varchar(20),
  property as varchar(50),
  value as varchar(50)
)

insert into css values('DEFAULT','a'       ,"color"          ,"#660033")
insert into css values('DEFAULT','a'       ,"text-decoration","none")
insert into css values('DEFAULT','a:active',"text-decoration","underline")
insert into css values('DEFAULT','a:hover' ,"text-decoration","underline")
I think its important to point out that this DB structure has other uses as well. This design is a basic "property bag". It could be used to store any number of properties for any number of reasons. I have used this before to contain properties for Applications written in C, VB, Java, and now ASP.

Step 2: CSS Script
All this code does is dump the values to the data stream, formatted as valid CSS. Note the 3rd or 4th line down (
Code:
response.contentheader = "text/css"
) this line is important for servers to know what they are seeing. It also assists the browser in identifying what it is looking at.

Changing the content header will also allow you to send other types of data including images or sounds.
[tt]
<%
[color blue]option explicit[/color]

[color green]'change the content header (from "text/html" to "text/css")[/color]
Response.ContentType = "text/css"

[color green]'declarations[/color]
dim curClass
dim style
dim sql
dim rs

[color green]'initialize[/color]
curClass = ""
style = request.QueryString("style")
set rs = server.createobject("ADODB.Recordset")

[color green]'build sql statement[/color]
sql = "select class, " & vbnewline & _
" property, " & vbnewline & _
" value " & vbnewline & _
"from cssClass c, " & vbnewline & _
" cssProp p " & vbnewline & _
"where c.style = p.style and " & vbnewline & _
" c.class = p.class and " & vbnewline & _

if(style = "")then
sql = sql & " style = 'default' " & vbnewline
else
sql = sql & " style = '" & style & "' " & vbnewline
end if

sql = sql & "order by p.precedence, c.class " & vbnewline

[color green]'open recordset[/color]
rs.cursorlocation = 3 'adUseClient
rs.open sql, Application("cnnStr")
set rs.activeconnection = nothing

'loop through each property
while(not rs.eof)
'if the class has changed with the new record
if(currClass <> rs("class").value)then
'if its not the very first record
if(currClass <> "")then
'close the old CSS class
response.write("}" & vbnewline & vbnewline)
end if
'change which class we are working on
currClass = rs("class").value
'open the new class
response.write(currClass & "{" & vbnewline)
end if
'note the property and its value
response.write(vbtab & rs("property").value & " : " & rs("value").value & ";" & vbnewline )

rs.movenext
wend
'close the last class
response.write("}" & vbnewline)

'clean up
rs.close
set rs = nothing

response.end
%>
[/tt]

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top