1. Tech

Your suggestion is on its way!

An email with a link to:

http://delphi.about.com/library/bluc/text/uc060901h.htm

was emailed to:

Thanks for sharing About.com with others!

RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
ISAPI tutorial for Delphi developers
Page 8: TPageProducers
 More of this Feature
• Pg 1: Intro to web-broker
• Pg 2: Using this tutorial
• Pg 3: Getting started
• Pg 4: Web Actions
• Pg 5: The first ISAPI app
• Pg 6: TWebRequestObject
• Pg 7: Request-responding
• Pg 9: Cookies made easy
• Pg 10: Stateless HTTP
• Pg 11: DB enabled apps
• Pg 12: FAQ
• Pg 13: DB apps - Part 1
• Pg 14: DB apps - Part 2
• Pg 15: DB apps - Part 3
 Join the Discussion
"Post your questions, concerns, views and comments to this article..."
Discuss!
 Related Resources
• Internet programming
• CGI and ISAPI with Delphi

As I mentioned in the previous page, Delphi 5 gives you another option when responding to the request. This is done by filling in the Producer property of the web action. However, as I also said, I would not cover this topic until we have seen what a PageProducer is. Well, in this chapter we are going to examine just this. The TPageProducer comes in many flavours, the most simple one TPageProducer and then more complex types that are Data-aware enabled (TDataSetPageProducer, etc). These will be covered once we have seen Database applications with ISAPI's.

The TPageProducer is actually a simple and yet very common and powerful component. In it's simplest for you can think of it as just a stringlist that contains HTML strings and acts like a template. It has very little properties/methods and only one event:

Property Description
DispatcherRefers to the Dispatcher. Normally you should not need to modify this property in an ISAPI application and leave the default Dispatcher that is created.

·HTMLDoc This is a string of HTML values

·HTMLFile This is an actual HTLM File. You cannot set both the HTMLDoc and HTMLFile property at the same time. Setting the HTMLFile property will clear the HTMLDoc value.

·Name, Tag As with other component

Method Description
·Content When you call content, the strings of the PageProducer will be parsed according to the template and the value will be returned.

·ContentFromStream Similar to the previous, but in this you read the contents from a stream that you pass as a parameter to the method.

·ContentFromString Again as before but read from a string

Event Description
·OnHTMLTag Everytime the PageProducer encounters an HTML transparent tag, it will fire this event (shortly we will see what an "HTML Transparent Tag" is.)

Let's see a very simple example of how to use the TPageProducer (example chapter8a). As you can see the only line that this

if (Request.QueryFields.Values['file']) <> '' then 
 ppRESULT.HTMLFile := Request.QueryFields.Values['file'] 
else begin
 with ppRESULT.HTMLDoc do begin
  Clear;
  Add(''); 
  Add('You did not specify a file'); 
  Add('Therefor, you are seeing the result of'); 
  Add('assigning a value to the HTMLDoc property'); 
  Add('of the TPageProducer');
  Add(''); 
 end;
end;
Response.Content := ppRESULT.Content ;

By now this code should seem simple enough to you. The only new thing is the assignments to the ppRESULT TPageProducer we have in this project. What we do is see if the Query contains a parameter calledfile. If so, we assign the value to the HTMLFile property. In other case, we assign a set of strings to the HTMLDoc property.

The last call is the same in both cases and call the Content method of the TPageProducer. In the same line we assign this value to the Response.Content property. Test this example and see the result.

NOTE: if you are using IIS make sure that the file parameter you pass in the URL has the correct permissions to be read by the IUSR_MACHINENAME.

Well that seemed easy enough. But you might ask yourself, what difference is there to this than the previous example? We could assign a file in the previous example aswell. Up to this point, there is no difference. This is also because we haven't used the full power of the TPageProducer yet.

Tags
Let's go back to the example 6A in chapter 6 for a minute. Do you remember this line of code:

Response.Content := 'Hello '+Request.ContentFields.Values['NAME_FIELD'] + ' ' + Request.ContentFields.Values['SURNAME_FIELD'] + ' how are you today?';

All it did was respond with a blank page with "Hello Name + Surname. How are you today?", where it took the name and surname from the values of the form we submitted. Let's take this one step further and use the TPageProducer to produce the same effect:

with ppRESULT.HTMLDoc do begin
 Clear;
 Add(''); 
 Add('Hello');
 Add(Request.ContentFields.Values['NAME_FIELD']+' '+
     Request.ContentFields.Values['SURNAME_FIELD']); 
 Add('How are you today?'); 
 Add('of the TPageProducer');
 Add('');
end;

This would produce the exact same result. Since this is an HTML page, we could spruce it up a bit and add some color, a few images and a border around the text. Imagine how tedious it would be to do all this? Throw the WYSIWIG HTML editor out the window and go back to hard-coding HTML tags. We wouldn't have to take it that far. We could actually design the HTML page with our favorite tool and then copy/paste into the source code of our project and adapt it to our needs. This seems ok and not too painful of a task at first, but there are two major problems:

1. What if you want to change the image? You would have to change the source code and then re-compile the DLL again.

2. I don't know about you but I'm not very good at designing web pages. In many companies, the web designer and the programmer are two different people and departments. This results in you asking the designer to create a page so you can copy the HTML source into your pascal code. No, thank you.

So how do we overcome these problems? Well as great as Delphi is, it just got greater. Delphi provides you with a what is called an HTML-transparent tag. As you know, tags in HTML are represented with"<" tag_name ">". In Delphi you can use tags that are transparent to the standard HTML by prefixing them with the # sign. A tag would therefor be represented with "<#" tag_name ">".

In the next example, I'm going to produce the exact same result as the previous one, but in this case with the use of tags. The HTML page would look something like this:

Hello <#NAME> <#SURNAME>. How are you today?

NOTE: When using a WYSIWYG HTML editor, make sure that once you have saved the page, the '<' and '>' symbols appear correctly. Since these are used in HTML code, most editors replace them with < and >.

If you examine the code for this example (Chapter8B.dpr), we have only one action which is waTAG. As before, we assign the the HTMLFile property and then call the method Content assigning the output to the Response. This method in turn will fire the OnHTMLTagEvent of the PageProducer, which looks like:

if TagString = 'NAME' then 
 ReplaceText := Request.ContentFields.
                  Values['NAME_FIELD'] 
else
 if TagString = 'SURNAME' then 
   ReplaceText := Request.ContentFields.
                  Values['SURNAME_FIELD']; 
end;

The TagString parameter contains the name of the Tag we put in our HTML file without the enclosing '<', '>' or '#'. The ReplaceText parameter is the text that the tag will be replace with.

This is a very simple example to show you how tags work. You can additionally pass parameters to the tags in the HTML file and process these parameters in your code. It's well worth the while to play around with the tags until you get confortable with them. For more information, look at the on-line help in Delphi.

Producer
In Page 7 I mentioned that in Delphi 5 each Web-action has a new property called the Producer. Now that we have seen what a PageProducer is and does, I can explain this new property. It's actually very simple and really doesn't need any explanation. Since nearly all responses to a request to your application imply the use of a PageProducer (in any of it's flavours, TDataSetPageProducer, etc. I'll explain these in Chapter 11), the guys at Borland probably decided to save us even more from writing code, by adding a property to each request that let's us assign the Producer that will provide the result. Look at the action Producer of example Chapter8b. It does exactly the same as the tag action.

Next page > Cookies made easy > Page 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC.
· NEXT ARTICLE: ADO Cursors - DB/10.
Chapter ten of the free Delphi Database Course for beginners. How ADO uses cursors as a storage and access mechanism, and what you should do to choose the best cursor for your Delphi ADO application.
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?

©2014 About.com. All rights reserved.