I've finished porting my Delphi Relax web scripting library to RAD Studio 10.2, adding Linux support. I also added a new set of unit tests. The library is available at https://github.com/marcocantu/DelphiRazor/ and we are considering adding it to GetIt as well. In two weeks I'm going to host a webinar covering the library, covering different scenarios of modern web development. Stay tuned for that.

In this blog post I want to focus on a 5 minutes demo showing Linux support. I'll probably expand the demo for the webinar. First of all, what is Delphi Razor? It is a web scripting notation heavily inspired by ASP.NET Razor notation. The Delphi tie in comes form the ability of the application to exposes objects, list of objects and datasets to the web page processing engine, by means of RTTI.

To build a simple database-driven the demo, I've created a console-dames WebBroker application, I've dropped an FDMemTable and an FDStanStorageBinLink component to the web module. This will allow me to load data from a local file (local on the web server). Next I've dropped an RlxRazorProcessor component and associated it with a local HTML file, by setting the InputFilename property to "./table.html".

I've also added a custom action to the web module, and added an event handler for it. You can see the design time components below:

Next, I've create a fairly trivial HTML file for the data. Notice this can be any HTML, based on Bootstrap (like I've done for other Delphi Razor projects) and integrating any JavaScript client library. This one is really a bare bone HTML file:

<h2>Employees</h2>
   
<ul>
@foreach (var emp in employee) {
  <li>@emp.FirstName @emp.LastName (@emp.PhoneExt)</li>
}
</ul>

What this does is iterate over the element called "employee" (which happens to ba a database table) and replicate the HTML within the curly braces for each record, creating a line item for each entry with the value of three fields. The for loop defines a local loop variable "emp" that is used in the loop to refer to the record. Drop that segment of HTML with script into a nice looking HTML page and you are in business.

The last bit is adding the code to load the database table and associate it with the name "employee" used in the script. Here is the complete code of the web action event handler:

      procedure TWebModule2.WebModule2WebActionItem1Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var
  filename: string;
begin
  filename := './employee.fds';
  if not FDMemTable1.Active then
      begin
    FDMemTable1.LoadFromFile(filename);
    FDMemTable1.Open;
  end;

  RlxRazorProcessor1.AddToDictionary('employee', FDMemTable1, False);
  Response.Content := RlxRazorProcessor1.Content;
end;

The binding of the internal data with the script happens by adding an entry in the processor dictionary. By copying its content to the output, the last line of the code will trigger the script processor.

Now compile and deploy to Linux, start it (like in the image below):

You'll be able to see the ugly HTML output in your browser:

Stay tuned for more information about the Delphi Razor webinar.