sgcHTML Components Inside Delphi WebStencils Templates

· Components
sgcHTML Components Inside Delphi WebStencils Templates

WebStencils is Embarcadero's server-side template engine for Delphi and C++Builder, introduced in RAD Studio 12.2. It lives in the Web.Stencils unit, with TWebStencilsProcessor and TWebStencilsEngine, and lets you write plain HTML files with an @ syntax for values, loops and conditions. It runs on WebBroker and RAD Server, and Embarcadero promotes it together with htmx.

sgcHTML builds web UI components from Delphi on top of Bootstrap and htmx, so the two fit naturally. This post shows how to drop sgcHTML cards, charts, grids and forms into a WebStencils template. The short version: it already works with the current sgcHTML release, with no extra sgc code.

sgcHTML components rendered by WebStencils templates in a Delphi WebBroker application. Also on YouTube.

One Property, One Line of Template

Every sgcHTML component has a public HTML property that returns its finished markup. WebStencils reads the public properties of any object you hand it, through RTTI. Put the two together and the integration is one call plus one line: AddVar the component on the processor, then write @\name.HTML in the template where the component should appear. There is no adapter class, no extra unit and no change to the library.

Here is the smallest complete program, a console application that renders a StatCard through a WebStencils processor:

program wst;

{$APPTYPE CONSOLE}

uses
  System.SysUtils, System.Classes, Web.Stencils,
  sgcHTML_Component_StatCard;

var
  oCard: TsgcHTMLComponent_StatCard;
  oProc: TWebStencilsProcessor;
  vOut: string;
begin
  oCard := TsgcHTMLComponent_StatCard.Create(nil);
  oProc := TWebStencilsProcessor.Create(nil);
  try
    oCard.Title := 'Revenue <Q4>';
    oCard.Value := '12,400';
    oProc.AddVar('card', oCard, False);
    vOut := oProc.ContentFromString(
      '<h1>@card.Title</h1>' + sLineBreak +
      '<div class="raw">@\card.HTML</div>');
    WriteLn(vOut);
  finally
    oProc.Free;
    oCard.Free;
  end;
end.
#include <vcl.h>
#pragma hdrstop
#include <tchar.h>
#include <stdio.h>
#include <Web.Stencils.hpp>
#include "sgcHTML_Component_StatCard.hpp"
#pragma link "sgcHTML_Component_StatCard"

int _tmain(int argc, _TCHAR* argv[])
{
	TsgcHTMLComponent_StatCard *oCard = new TsgcHTMLComponent_StatCard((TComponent*)NULL);
	TWebStencilsProcessor *oProc = new TWebStencilsProcessor(NULL);
	try
	{
		oCard->Title = "Revenue <Q4>";
		oCard->Value = "12,400";
		oProc->AddVar("card", oCard, false);
		UnicodeString vOut = oProc->ContentFromString(UnicodeString("<h1>@card.Title</h1>") +
			sLineBreak + "<div class=\"raw\">@\\card.HTML</div>");
		printf("%s\n", AnsiString(vOut).c_str());
	}
	__finally
	{
		delete oProc;
		delete oCard;
	}
	return 0;
}

Both programs were compiled and run on Delphi and C++Builder 12 and 13. The <h1> prints the card title as text, and the <div> receives the full markup of the StatCard.

The Backslash Matters: Raw Markup Versus Text

WebStencils encodes values by default, which is the safe choice for text. That is why the template uses two different forms:

Use the backslash form only for the HTML property of a component. It does not open a hole for user data, because sgcHTML components escape their own text values before they build their markup. A title set to Revenue <Q4> comes out inside the card as Revenue &lt;Q4&gt;. Plain values you print yourself, such as @card.Title, keep the normal @ form and WebStencils encodes them.

Layouts, Loops and Conditions Around the Components

The rest of WebStencils works as usual. @LayoutPage and @RenderBody give every page a shared layout, @ForEach loops over your data, @if handles conditions and @query reads URL parameters. The components simply sit where you place them. This is the top of templates\dashboard.html from the demo described below:

@LayoutPage layout
<h1 class="h3 mb-3">Sales dashboard</h1>
<div class="row g-3 mb-4">
  <div class="col-sm-6 col-lg-3">@\revenueCard.HTML</div>
  <div class="col-sm-6 col-lg-3">@\ordersCard.HTML</div>
  <div class="col-sm-6 col-lg-3">@\customersCard.HTML</div>
  <div class="col-sm-6 col-lg-3">@\averageCard.HTML</div>
</div>
<div class="row g-3">
  <div class="col-lg-7">@\chart.HTML</div>

The Delphi side creates the four StatCards and the chart and registers each one with AddVar under the name the template uses. The template decides where they go, and the Bootstrap grid decides how they wrap on a phone.

Interactive Components: Grids and Forms Over htmx

Static components only need the template. Interactive ones, such as a grid with server paging and sorting or an htmx form, also need something on the server to answer the requests they send. For that, host TsgcHTMX_Engine_Server_WebBroker in the same web module.

The engine answers the htmx requests through its router. A route such as /htmx/orders returns the grid fragment for the requested page and sort order, and only that fragment is swapped in the browser. The engine also serves /bootstrap.min.css and /htmx.min.js, so the pages load them from the same application. WebStencils keeps rendering the full pages, and the sgcHTML engine handles the fragments. The WebBroker hosting page explains the engine in detail.

The 44.WebStencils Demo

The demos Demos\Delphi\HTML\44.WebStencils and Demos\CBuilder\HTML\44.WebStencils put everything together in one WebBroker application with three pages:

The demos ship with sgcHTML 2026.12, the next release, described in the release 2026.12 post. The technique itself needs nothing new: it works today with the current sgcHTML release, and only the demo projects arrive with 2026.12.

Requirements

WebStencils needs Delphi or C++Builder 12.2 or newer. It is not available in 12.1 or older. In the RAD Studio 13.2 feature matrix it is part of the Professional, Enterprise and Architect editions, and Community Edition includes it from 13.0. On the sgcHTML side there is no minimum beyond a current release, because the integration only uses the public HTML property that every component already has.

Learn More

The sgcHTML WebStencils page collects the full write-up, and the online help has the reference topic. To try it in your own WebStencils project, download the sgcHTML trial.

Questions, feedback or help wiring sgcHTML into your templates? Get in touch. You will get a reply from the people who wrote the code.