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!

How can I have an easy template system?

Templates

How can I have an easy template system?

by  vercesi  Posted    (Edited  )
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.:)
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