Using Page Producers from an Action Item

From RAD Studio
Jump to: navigation, search

Go Up to Using Page Producer Components


A typical use of a page producer component uses the HTMLFile property to specify a file containing an HTML template. The OnAction event handler calls the Content method to convert the template into a final HTML sequence:

procedure WebModule1.MyActionEventHandler(Sender: TObject; Request: TWebRequest;
   Response: TWebResponse; var Handled: Boolean);
begin
   PageProducer1.HTMLFile := 'Greeting.html';
   Response.Content := PageProducer1.Content;
end;
void __fastcall WebModule1::MyActionEventHandler(TObject *Sender,
   TWebRequest *Request, TWebResponse *Response, bool &Handled)
{
  PageProducer1->HTMLFile = "Greeting.html";
  Response->Content = PageProducer1->Content();
}

Greeting.html is a file that contains this HTML template:

<HTML>
<HEAD><TITLE>Our Brand New Web Site</TITLE></HEAD>
<BODY>
Hello <#UserName>!  Welcome to our Web site.
</BODY>
</HTML>

The OnHTMLTag event handler replaces the custom tag (<#UserName>) in the HTML during execution:

procedure WebModule1.PageProducer1HTMLTag(Sender : TObject;Tag: TTag;
   const TagString: string; TagParams: TStrings; var ReplaceText: string);
begin
   if CompareText(TagString,'UserName') = 0 then
      ReplaceText := TPageProducer(Sender).Dispatcher.Request.Content;
end;
void __fastcall WebModule1::HTMLTagHandler(TObject *Sender, TTag Tag,
   const AnsiString TagString, TStrings *TagParams, AnsiString &ReplaceText)
{
  if (CompareText(TagString,"UserName") == 0)
    ReplaceText = ((TPageProducer *)Sender)->Dispatcher->Request->Content;
}

If the content of the request message was the string Mr. Ed, the value of Response.Content would be

<HTML>
<HEAD><TITLE>Our Brand New Web Site</TITLE></HEAD>
<BODY>
Hello Mr. Ed!  Welcome to our Web site.
</BODY>
</HTML>

Note: This example uses an OnAction event handler to call the content producer and assign the content of the response message. You do not need to write an OnAction event handler if you assign the page producer's HTMLFile property at design time. In that case, you can simply assign PageProducer1 as the value of the action item's Producer property to accomplish the same effect as the OnAction event handler above.

See Also