Dynamic CSS (ASP)

They have: 330 posts

Joined: Apr 2000

I would like to create a page with a dynamic stylesheet. The customer using the application will have somewhat of a control panel where they can choose the colors and fonts to make the webpage and I want those choices to write directly to the CSS.

The only 3 ways I can think of to make this happen are:
1. Run from DB - I want to minimize the DB usage in this application, this option is no good.
2. Create include files for each stylesheet variable and use FSO to write to the inc files. - No good because this would require a seperate inc for each attribute and for a whole application that is just rediculous.
3. Somehow run queries with FSO and write directly to the CSS file. - This is my preferred way to do it, but that's why I'm here... How? Is this possible? Can I query a text file for specific data and then replace specific text in that text file?

Please let me know if I'm making this harder than it really is. If there is another way to do this please let me know.

Thanks for any help.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

why no databases?

They have: 330 posts

Joined: Apr 2000

I am trying to minimize the use of databases because the application is already required to reference different SQL statements up to 4 times per page. Some people would say "if you're already openning 4, why not open 1 more?" but that isn't the way to make it work.

I could open a database once at the beginning of the session and then save sessions but that is more strain on the server than anything because of the number of variables I will have to save.

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

If you're able to rely on cookies or sessions then you could always pull them from there, then if they're permanent cookies they'll have the same settings next time.

PJ | Are we there yet?
pjboettcher.com

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

You'r running 4 queries per page, or opening 4 connections per page?

4 queries per page is nothing to worry about. The time it takes to run a query is tenths or hundredths of a second.

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 330 posts

Joined: Apr 2000

You're right, 4 queries is nothing to worry about, but it's strain on a server that is not necessary if this is written correctly.

I believe I have created a way in FSO to make the proper changes directly to the style.css file. If this works it will also be a great tool to use in the future for other applications.

Here's the idea, tell me if you think it's possible or if you know of a better way.

Create control panel page where the user chooses which portion of the CSS file they want to change. We'll assume they choose the class "Header". They then have the option of changing the font size, font family, font-weight, color, etc. The following page once the form is submitted will open the CSS file as a text file, Replace the string starting with Header {...} and then resave it to the correct directory.

Is this not a good way to do it?

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

It's just my opinion but I think you're making this more complex than it needs to be. Accessing the file system (FSO) is also a very resource intensive process, having an efficient stored procedure is less of a drain on a server then multiple access to the file system.

It's your call, but if it was me I would create 1 ASP page that would serve as the style sheet (included at the top of every page) and pass all the style sheet variables in session or cookie objects to avoid going back to the db.

PJ | Are we there yet?
pjboettcher.com

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I would use the common SSI file like Peter, but I would hit the DB again.

It's more of a system drain to use the FSO than hit the DB (IMHO). Just compare UBB's flat file storage to vBulletin's DB storage.

Why are you so affraid to use the DB? Are you running this site off Access?
How much traffic are you getting?

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 330 posts

Joined: Apr 2000

Yes, this is running on Access. I have been working on a version for SQL but I am terrible at SQL server right now so it's doing terrible. The traffic is next to nothing today but I'm building it for a department with over 250 employees accessing this at least 2 - 3 times a day. With this many hits at possibly close to the same time every hit into the DB is dangerous.

FSO will only be used when the Admins of the site want to change the look. Usually this would be done during the initial setup process and maybe a few times throughout the year.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

ahhh, I see

They have: 330 posts

Joined: Apr 2000

I have everything working!

... except one thing.

So far I have created the stylesheet which works great. I can make changes through the web-based form and it is flawless. Except for when I want to change it a 2nd time.

The process I'm using doesn't seem to be saving the data like I hoped. Below you will find my source code and my troubleshooting.

<%
Dim FSO
Dim txtStream

Set FSO = Server.CreateObject("Scripting.FileSystemObject")

Set txtStream = FSO.OpenTextFile(Server.MapPath("../style.css"))

Image = Request.Form("Image")
Font = Request.Form("Font")
Size = Request.Form("Size")
Weight = Request.Form("Weight")
Color = Request.Form("Color")

varNewLine = "{font-family: " & Font & "; font-size: " & Size & "; font-weight: " & Weight & "; color: " & Color & "; Background-Image: url(Images/" & Image & "); height: 20px;}"

Do While Not txtStream.AtEndOfStream
strLine = txtStream.ReadLine
If strLine = ".Footer" Then
strNewStyle = strLine & Chr(13)
strLine = txtStream.ReadLine
strNewStyle = varNewLine & Chr(13)
Else
strNewStyle = strLine & Chr(13)
End If
Loop

Set NewStyle = FSO.CreateTextFile(Server.MapPath("../style.css"))

NewStyle.WriteLine (strNewStyle)

NewStyle.Close

Set NewStyle = Nothing
Set txtStream = Nothing
Set FSO = Nothing
%>
'

The problem I'm having is once it has been used once the format I'm using creates the string "strNewStyle" but it seems to be a never ending string even though I'm using Chr(13) after each line.

If there is a way to create an end of each line I would appreciate your help in finding it.

Thank you.

Art Sapimp

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

Try replacing the Chr(13) with VbCrLf.

They have: 330 posts

Joined: Apr 2000

When it was writing the line it automatically entered a carriage return so after trying to use it 3 or 4 times I noticed the style.css had 3 or 4 blank lines between each line of code.

I have removed the Chr(13) and it is visually exactly the same as the style.css when it's done. It just doesn't act like it.

Someone pointed out the fact that since it's being created by FSO it's possible it's not in the right format ANSI vs Unicode. Do you know how I would ensure it's being created correctly? And which format should I be using?

Thanks for any help.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.