ASPCompiler ASPC Parser as HTML template
ASP Compiler 1.1 documentation is under development. See also the examples.

See also the sample HTMLTemplate.

How the HTML templates work?

The applications could do a lot of interesting work over a HTML file by using regular expressions or other text search/replace methods. But as the complexity grows the task becomes more difficult and more difficult. It is much more easier to perform some replacement and manipulation tasks over an object model of a page instead of treating it as plain  text. This is what the TextEmbedParser does. The original page (template) is loaded in a string by the application (it could be loaded from a file, DB or another source) and passed to the pareser's ParseString method. It returns the object model of the page represented by VarDictionary collections. The result returned is a typical "tree" structure, which means the retuned collection is the root of the document and it contains sub-collections to represent the particular nodes/sections of the document.

Then the script (ASP for example) modifies the received object structure by deleting/adding/changing the nodes and passes it back to the parser for regeneration. The parser regenerates the document over the modified object tree and then the script could output it or use the created document for other purposes (save it to a file, keep it in some DB or do something else). 

What are the benefits of the templates?

This allows an ASP page for example to work over an abstract document instead of binding to a constant design. Thus the site design could be changed by changing the templates and the page's code will not need any change! The only requirement for the new templates will be the elements the script expects in them. Depending on the application needs and future expectations the ASP page that uses templates could be implemented to handle different templates (e.g. different structures) or only a single template model. Supporting single model requires less code almost the same amount of code as any typical ASP page, handling unexpected situations, of course, requires more efforts. Anyway the object structure of the document allows the developer to write code that inspects the actual template and determines the situation. Thus the template handling flexibility depends on the developer decision only and the developer could decide where to stop - write something for minutes or create complex code capable to fill wide range of different structured templates. Even in the simplest case the flexibility is far beyond the typical ASP page!

We will use the following sample page in the discussion below::

<HTML>
<HEAD><TITLE>Some title</TITLE></HEAD>
<BODY>
Some text
<CUSTOM ID="Area1">
  Some text
  <CUSTOMFIELD ID="SomeID">
  Some Text
</CUSTOM>
</BODY>
</HTML>

About the object model of the page.

TextEmbedParser is part of ASPC. The object model when used for HTML templates is a subset of the page object model for the Compile time scripts. In other words in HTML template case only the tags added through AddTag method are used and there are no ASPC predefined tags. Thus the page object model consist of HTML tags represented as nodes. The object model includes only the tags you specify! Everything else is treated as plain text. Therefore the object model of a HTML page is much more simple than the DHTML object model for example. It includes only the parts that require your attention (e.g. the script configures the parser to recognize only the elements of some interest). The resulting object model will be set of nodes that represent the tags you specified and plain text nodes. Let's take some examples:

Dim parser
Set parser = Server.CreateObject("newObjects.utilctls.TextEmbedParser")
parser.AddTag "TITLE", True, False, False, True, "", ""
Set model = parser.ParseString(prevoouslly_obtained_text_of_the_template)

Applied over the sample page above this will return object structure like this:

  • Root collection
    • model("__class") = "plain/text"
      that contains the text from <HTML> to the <TITLE> tag (but except it)
    • model("__class") = "TITLE"
      • model("__class") = "plain/text"
        The text "Some title"
    • model("__class") = "plain/text"
      Everything after the </TITLE> tag

If we add two more tags:
parser.AddTag "CUSTOM", True, False, False, False, "", ""
parser.AddTag "CUSTOMFIELD", False, False, False, True, "", ""

The object model will contain more nodes:

  • Root collection
    • model("__class") = "plain/text"
      that contains the text from <HTML> to the <TITLE> tag (but except it)
    • model("__class") = "TITLE"
      • model("__class") = "plain/text"
        The text "Some title"
    • model("__class") = "plain/text"
      Everything after the </TITLE> tag to the beginning of the <CUSTOM> tag
    • model("__class") = "CUSTOM"
      • model("__class") = "plain/text"
      • model("__class") = "CUSTOMFIELD"
      • model("__class") = "plain/text"
    • model("__class") = "plain/text"
      After the </CUSTOM> tag

How to modify the template/object structure?

The technique is the same as in ASPC if a CTS is used. Thus you may look also at CTS chapters. The modification is based on node modifications. The certain node could be changed, HTML attributes could be added to the attributes existent in the template and, of course, the content can be changed. Not that content changes are made by altering plain/text nodes. For example if you need to change the title of the page (first sample tree) you need first to obtain the TITLE node from the tree:

Dim title
Set title = model.FindByValue("__class","TITLE")(1)

And then get the inner plain/text node and modify it "__content" item. This item contains the text in it.

Dim titleText
Set titleText = title.FindByValue("__class","plain/text")(1)
titleText("__content") = "New title"

Pay attention to the FindByValue and FindByName methods of VarDicitonary object.

The regeneration of the page could look as simple like this:

Response.Write parser.GenerateDoc(model)

If you need to replace part of the template with the same part repeated several times the procedure will include creating copy of that part. To do so you use the Clone method of the VarDictionary object. Let's take as an example the second tree (that uses CUSTOM and CUSTOMFIELD tags). Suppose the CUSTOM tag with ID="Area1" represents the part we want to repeat (may be for each record returned by a query). First we need to find the node corresponding to the tag:

Set area = mode.FindByName("Area1")(1)

ID attribute (if present) determines the name of the node. Thus using ID=someName always makes it easier to find certain tag. It can be found by some other characteristics but this may cause problems if the template has been changed incorrectly. Now we need a copy:

Set areaTemplate = area.Clone

We will reproduce this copy several times and put it in the original area. Thus the original area should be cleaned up in order to not show the empty template itself:

area.Clear

We will repeat the next step several times. The actual cycle will be different depending on the page nature. Thus here only the code inside the cycle is listed:

Set tempArea = areaTemplate.Clone
' New copy for the current step
Set theField = tempArea.FindByName("SomeID")(1)
' Finding the field in the repeated area
theField("__class") = "plain/text"
theField("__content") = content_for_this_step
' Replace the field with the content for the current step.
' We are changing its class (and role) to plain/text and it will appear in this copy as
' text
area.Add tempArea
' Adding current copy to the original area

See the sample HTMLTemplate.to see this in action. The sample contains 3 steps simple, repeating a part of the template and repeating a part of the template in repeated part. The ASP code is commented - you can review the steps as separate code samples.

newObjectsSite4All.nl Copyright 2001-2006 newObjects [ ]