-
Notifications
You must be signed in to change notification settings - Fork 22
Using the RazorEngine
Rather than using individual RazorProcessor components for the various HTML files you can use the engine component to create such files. In the RazorWebBroker demo this is triggered by the following default actions:
procedure TWebModule13.WebModule13DefaultHandlerAction(
Sender: TObject; Request: TWebRequest;
Response: TWebResponse; var Handled: Boolean);
var
Found: Boolean;
begin
Response.Content :=
RlxRazorEngine1.ProcessRequest (Request, Found);
Response.ContentType := 'text/html';
if not Found then
begin
Response.StatusCode := 404;
Response.ReasonString := 'File not found';
end;
end;
In this case the engine will look for a file matching the request path, or use menu.html if no path is provided (i.e. the use is asking for /). This fixed value (menu.html) will be turned into a parameter. If the extension is .htm it will be converted to .html.
The engine can use the OnObjectForPath event handler to add dictionary objects to the current processors, depending on the path that was passes as parameter (very specific demo below).
In case of a path like /test/foo.html the Engine component will split it in two elements. The first part of the path (test) will be used to refer to the actual file (test.html), while the second (foo), becomes a parameter used to refer to an actual object. In the OnObjectForPath event handler these are passed as pathinfo (the filename) and pathparam (the extra element).
So in the code below, a URL like /buy/delphi.html, is turned into a call to buy.html passing delphi as parameter to the TProduct.Search class method, which does a database lookup and returns an ActiveRecord object with the data. But that's for a following section, not written yet.
This code is from a different demo:
procedure TWebModule2.RlxRazorEngine1ObjectForPath(Sender: TObject;
const pathinfo, pathparam: string;
razorProc: TRlxRazorProcessor);
var
product: TProduct;
begin
if SameText (pathInfo, 'buy.html') then
begin
product := TProduct.Search (
'code = ' + QuotedStr(pathParam)) as TProduct;
razorProc.AddToDictionary('product', product);
In most cases you'll use only an Engine component to manage a complex web site. This OnObjectForPath event handler might probably be replaced with a more sophisticated mechanism (an engine-level dictionary?).