INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Templates

How can I have an easy template system? by vercesi
Posted: 20 May 02 (Edited 22 May 02)

Hi there!

Before I begin, let me state that this idea came originally from a coleague, Nuno Baptista, with whom I'm proud to work with. Having said that and apologizing for any spelling errors, let's begin.

What's one of the worst things developers have to deal with? I'm guessing, besides clients , design. And the same is valid in reverse. To properly conciliate design and development is sometimes a major headache. So, how on earth do we solve this? Templates of course. PHP users are very familiar with them. How about ASP users? I really haven't found a easy an fast way to do it until I began at my current job.

Let's take it by steps.

1st - The design issue

Have your designer fun around all they want and then insert html comments were the information should go. For example:


<html>
<head>
<title><!--title--></title>
<link rel="stylesheet" type="text/css" href="<!--style-->">
</head>
<body>
<!--information-->
</body>
</html>

Consider this as file1.html

2nd - The function that keeps it together.

'CODE START

Function ReadTemplateFile (FilePath, FileToOpen)

Dim fs, f, line

line = ""

set fs=CreateObject("scripting.filesystemobject")
If fs.FileExists(Request.ServerVariables(4) & FilePath & FileToOpen) Then
    set f=fs.OpenTextFile(Request.ServerVariables(4) & FilePath & "\" & FileToOpen)
    Do While not f.AtEndOfStream
    line= line & f.ReadLine()
        line= line & VbCrLf
    Loop
    f.Close
else
    line= "The file " & Request.ServerVariables(4) & FilePath & FileToOpen & " was not found!"
end if

ReadTemplateFile = line

End Function

'CODE END

Save this function into an include file, eg: functions.asp

3rd - The glue that makes it work

Create your asp file. Could be a search script with access to a database or anything else. Anything you want or need.

Finally, include the function file in you asp file and make it work:

'START CODE

<!--#INCLUDE Virtual="/includes/functions.asp"-->
<%
Dim result
result = ReadTemplateFile ("mypath\", "file1.html")

'DO WHATEVER YOU HAVE TO
Dim myTitle
Dim myStyle
Dim myInfo

'REPLACE COMMENTS WITH DYNAMIC INFO
result = replace (result, "<!--title-->", myTitle)
result = replace (result, "<!--style-->",myStyle)
result = replace (result, "<!--information-->", myInfo)

'SEND THEM TO THE BROWSER
Response.Write(result)
%>

'END CODE


And you're all done. Best of all, everytime design changes, you only have to see were the info has to be replaced and also replace the html file name and/or path if needed.

Was it easy enough? I though so.

Back to Microsoft: ASP (Active Server Pages) FAQ Index
Back to Microsoft: ASP (Active Server Pages) Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close