RAD Studio |  |

Using Page Producers from an Action Item
See Also
Collapse All

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:

Copy Code

    procedure WebModule1.MyActionEventHandler(Sender: TObject; Request: TWebRequest;
       Response: TWebResponse; var Handled: Boolean);
    begin
       PageProducer1.HTMLFile := 'Greeting.html';
       Response.Content := PageProducer1.Content;
    end;

Copy Code

    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:

Copy Code

    <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:

Copy Code

    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;

Copy Code

    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

Copy Code

    <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

HTML Templates

Responding to Request Messages with Action Items

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!