Home » ASP » Article
Parsing Dynamic Layouts
|
Article by: |
Imran Salahuddin Khan (1/17/2005) |
|
Sponsored by: |
FindMyHosting - Web Hosting Search
|
Summary:
|
A way for keeping static and dynamic contents separate and to parse the dynamic contents for the variables inside the layout string. |
|
Viewed: 10181 times |
Rating (2 votes): |
|
1.5 out of 5 |
|
|
Parsing Dynamic Layouts
Download Project (1Kb)
Dynamic contents on any page are always combination of HTML and Dynamic Values, Dynamic values are obtained either using client side scripts or server side. They may be from database, file or anywhere. But always displayed on the page with some HTML around it.
For example:
<Table>
<tr>
<td>First Name</td>
<td><%=RS.Fields("FirstName").value</td>
</tr>
<tr>
<td>Last Name</td>
<td><%=RS.Fields("LastName").value</td>
</tr>
</table>
In the above code, a table of two columns two rows is displaying firstname and lastname fields values. It has always been a big trouble when changing layouts, you open page code, change the HTML Layout and then you must change your server side code (which fetches dynamic values). I am sharing a way which i use when developing dynamic pages either it is PHP, ASP or ASP.Net.
Description:
-----------
1. Store all dynamic areas (XHTML format) in a XML file. All dynamic areas of pages can be stored in one XML file.
2. The XHTML, inside the XML, will have some variables, you can use your own way, but i quote them with tild signs like ~FirstName~.
3. This way you can seperate dynamic areas from the static contents on page. and if sometimes you wish to change the layout you will have to design the layout using any HTML editor and will have to put those variables as discussed in point 2.
4. Use any collection store keys and values, i prefer dictionary object in ASP 3.0 or namevaluecollection in ASP.Net.
5. Keys in collection will be same as variable names. suppose you had a variable ~FirstName~ so you will make a key in collection FirstName.
6. In the end, you will just read the XHTML from XML and will parse it. And you will get HTML in result which will have values instead of variable with other HTML.
A Real Example:
----------------
Below is the class, i have written for you, keep it in a separate asp file and include whereever you want to use its object.
ParseLayout Class: parselayout.asp
-------------------------------------
Class ParseLayout
' A variable to hold layout string
Private StrLayout
'This property returns the StrLayout variable
Public Property Get GetLayout()
GetLayout=StrLayout
End Property
' This property will be used to set the Strlayout to the given string
Public Property Let SetLayoutStr(str)
StrLayout=str
End Property
' This Sub will be used to the StrLayout variable and it gets it string from the xml
Public Sub SetLayoutXML(xPath,xmlFilePath)
Set myDoc = Server.CreateObject("Microsoft.XMLDOM")
myDoc.async = False
myDoc.Load (xmlFilePath)
If myDoc.parseError.errorCode <> 0 Then
StrLayout=""
Else
Dim NodeName
NodeName=Mid(xPath,InstrRev(xPath,"/index.html")+1,len(xPath))
Set Nodes = myDoc.getElementsByTagName(NodeName)
If Nodes.length > 0 Then
Set Node = Nodes.item(0)
StrLayout=Node.XML
Else
StrLayout=""
End If
End If
Set Nodes=Nothing
Set Node=Nothing
Set myDoc=Nothing
End Sub
' In the last Parse will put all values in the layout.
Public Function Parse(dictObj)
If isobject(dictObj) Then
For Each key in dictObj
If instr(StrLayout,"~"&key&"~") > 0 Then
StrLayout=replace(StrLayout,"~"&key&"~",dictObj(key))
End If
Next
End If
Parse=StrLayout End Function
End Class
The second thing, an XML file holding XHTML for your dynamic part(s) of page(s).
Layouts XML: layouts.xml
---------------------------
<Layouts>
<Sample>
<table width="400" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Item No </td>
<td>~ItemNo~</td>
</tr>
<tr>
<td>ItemName</td>
<td>~ItemName~</td>
</tr>
<tr>
<td>OurPrice</td>
<td>~OurPrice~</td>
</tr>
<tr>
<td>RetailPrice</td>
<td>~RetailPrice~</td>
</tr>
</table>
</Sample>
</Layouts>
The above XML file has a node named "Sample", you can create more like this but node name should be unique. Each node like this will have XHTML inside. In the end, you will be implementing it in your actual page code.
Page Code: dynamicContents.asp
-----------------------------------
<!-- #include file="/ParseLayout.html" -->
<%
Dim NameValues
Set NameValues=Server.CreateObject("Scripting.Dictionary")
With NameValues
.Add "ItemNo","1"
.Add "ItemName","P4, Intel Processor"
.Add "OurPrice","200"
.Add "RetailPrice","300"
End With
Set ObjPL=new ParseLayout
ObjPL.SetLayoutXML "/Layouts/Sample",Server.MapPath("/Layouts.xml")
ObjPL.Parse(NameValues)
response.write ObjPL.GetLayout
Set NameValues=Nothing
%>
You have seen how we obtain a parsed HTML string from .GetLayout property of class ParseLayout. Now you can print this string wherever you wish to.
Article by Imran Khan http://itzimran.cjb.net
|
|
View highlighted Comments
User Comments on 'Parsing Dynamic Layouts'
|
|
|
To post comments you need to become a member. If you are already a member, please log in .
RELATED ARTICLES |
ASP Format Date and Time Script by Jeff Anderson
An ASP script showing the variety of date and time formats possible using the FormatDateTime Function. |
 |
Creating a Dynamic Reports using ASP and Excel by Jeff Anderson
A simple way to generate Excel reports from a database using Excel. |
 |
Create an ASP SQL Stored Procedure by Jeff Anderson
A beginners guide to setting up a stored procedure in SQL server and calling it from an ASP page. |
 |
ASP Shopping Cart by CodeToad Plus!
Complete source code and demo database(Access, though SQL compatible) to an ASP database driven e-commerce shopping basket, taking the user through from product selection to checkout. Available to CodeToad Plus! Members |
 |
Email validation using Regular Expression by Jeff Anderson
Using regular expression syntax is an exellent way to thoroughly validate an email. It's possible in ASP. |
 |
Creating an SQL Trigger by Jeff Anderson
A beginners guide to creating a Trigger in SQL Server |
 |
The asp:checkbox and asp:checkboxlist control by David Sussman, et al
Checkboxes are similar to radio buttons, and in HTML, they were used to allow multiple choices from a group of buttons. |
 |
ASP.NET Forum Source Code by ITCN
Complete open source website Forum and Discussion Board programmed in Microsoft dot Net 1.1 Framework with Visual Basic. |
 |
The asp:listbox control by David Sussman, et al
The next HTML server control that we'll look at, <asp:listbox>, is very much related to <asp:dropdownlist>. |
 |
Concatenate strings in sql by Jeff Anderson
A brief introduction to concatenating strings in an sql query (using SQL server or access databases). |
 |
|
|